daily notification

Hi,
I want to push notification at specific time on daily in my my app. I use xamarin and build a cross platform app. Please help me?
Thanks.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    If you need a local notification you can take a look there

    https://github.com/edsnider/Xamarin.Plugins/blob/master/README.md

    There is a notifyTime parameter

  • oflu1071oflu1071 TRMember

    I want you to repeat notification every day. What do i need to do for it?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Notifier.Current.Show("You've got mail", "You have 793 unread messages!");
    Notifier.Current.Show("Good morning", "Time to get up!", 1, DateTime.Now.AddDays(1);
    Notifier.Current.Cancel(1);

    Maybe the GoodMorning one

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Without cancel

  • oflu1071oflu1071 TRMember

    this one is add day to now time. So notification to be display at tomorrow. but not display two days later. I want to displaying every day of week.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think it continues to notify till you cancel it

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    The Notifier plugin doesn't support repeating notifications. I don't know of a cross-platform plugin that does. You'll need to implement the notifications natively on each platform and use a dependency service to call the implementation from your PCL.

  • LearnEverythingLearnEverything USMember ✭✭✭

    2016 I used this code and notify daily, I dont know for latest android

    Manifest

     <uses-permission android:name="android.permission.WAKE_LOCK" />
      <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    

    AlarmReceiver

    using Android.App;
    using Android.Content;
    using Android.Graphics;
    using Android.Support.V4.App;
    
    namespace XamarinAndroid.Core
    {
        [BroadcastReceiver]
        [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)]
        public class AlarmReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
    
                ////var message = intent.GetStringExtra("message");
                ////var title = intent.GetStringExtra("title");
    
    
                string message = "This is a sample message";//Replace with your notification message
                string title = "this is a sample title";//Replace with your notification Title
    
                var notIntent = new Intent(context, typeof(MainActivity));
                var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
                var manager = NotificationManagerCompat.From(context);
                var style = new Notification.BigTextStyle();
                style.BigText(message);
                int resourceId;
                resourceId = Resource.Drawable.NotiIcon;
                //    resourceId = Resource.Drawable.icon;
                var wearableExtender = new Notification.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, resourceId));
                //Generate a notification with just short text and small icon
                var builder = new Notification.Builder(context, "channel_id")
                .SetContentIntent(contentIntent)
                .SetSmallIcon(Resource.Drawable.NotiIcon)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetStyle(style)
                .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
                .SetAutoCancel(true)
                .Extend(wearableExtender);
                var notification = builder.Build();
                manager.Notify(0, notification);
    
            }
    
        }
    }
    

    Start Daily Notification

    static Toast repeating;
            static long halfday = 43200000;
            //long fifteenSeconds = 5 * 1000;
            public static void StartDailyNotification(Context context, Type alarmReceiverType)
            {
                // When the alarm goes off, we want to broadcast an Intent to our
                // BroadcastReceiver.  Here we make an Intent with an explicit class
                // name to have our own receiver (which has been published in
                // AndroidManifest.xml) instantiated and called, and then create an
                // IntentSender to have the intent executed as a broadcast.
                // Note that unlike above, this IntentSender is configured to
                // allow itself to be sent multiple times.
                var intent = new Intent(context, alarmReceiverType);
                var source = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
    
                // Schedule the alarm!
                var am = (AlarmManager)context.GetSystemService(Context.AlarmService);
                am.SetRepeating(AlarmType.ElapsedRealtimeWakeup,
                        SystemClock.ElapsedRealtime() + halfday,
                        halfday,
                        source);          
            }
        }
    
  • LearnEverythingLearnEverything USMember ✭✭✭

    Update 2019 work as repeating alarm

    using System;
    using Android;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Support.V4.App;
    using App1;
    using Java.Lang;
    
    namespace LocalNotificationDemo.Droid
    {
    
        public class LocalNotificationService
        {
            readonly DateTime _jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            internal static string _randomNumber;
            public void LocalNotification(int id, DateTime notifyTime)
            {
    
                //long repeateDay = 1000 * 60 * 60 * 24;       
                long repeateForMinute = 60000; // In milliseconds      
                long totalMilliSeconds = (long)(notifyTime.ToUniversalTime() - _jan1st1970).TotalMilliseconds;
                if (totalMilliSeconds < JavaSystem.CurrentTimeMillis())
                {
                    totalMilliSeconds = totalMilliSeconds + repeateForMinute;
                }
    
                var intent = CreateIntent(id);
                Random generator = new Random();
                _randomNumber = generator.Next(100000, 999999).ToString("D6");
    
                var pendingIntent = PendingIntent.GetBroadcast(Application.Context, Convert.ToInt32(_randomNumber), intent, PendingIntentFlags.Immutable);
                var alarmManager = GetAlarmManager();
                alarmManager.SetRepeating(AlarmType.RtcWakeup, totalMilliSeconds, repeateForMinute, pendingIntent);
            }
    
            public static void Cancel(int id)
            {
    
                var intent = CreateIntent(id);
                var pendingIntent = PendingIntent.GetBroadcast(Application.Context, Convert.ToInt32(_randomNumber), intent, PendingIntentFlags.Immutable);
                var alarmManager = GetAlarmManager();
                alarmManager.Cancel(pendingIntent);
                var notificationManager = NotificationManagerCompat.From(Application.Context);
                notificationManager.CancelAll();
                notificationManager.Cancel(id);
            }
    
            public static Intent GetLauncherActivity()
            {
    
                var packageName = Application.Context.PackageName;
                return Application.Context.PackageManager.GetLaunchIntentForPackage(packageName);
            }
    
    
            public static Intent CreateIntent(int id)
            {
    
                return new Intent(Application.Context, typeof(ScheduledAlarmHandler))
                    .SetAction("LocalNotifierIntent" + id);
            }
    
            private static AlarmManager GetAlarmManager()
            {
    
                var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;
                return alarmManager;
            }
    
        }
    
        [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, 
            Priority = (int)IntentFilterPriority.LowPriority)]
        [BroadcastReceiver(Enabled = true, Label = "Local Notifications Broadcast Receiver")]
        public class ScheduledAlarmHandler : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                //Generating notification    
                string title = "Title";
                string message = "Message";
                Notifier notifier = new Notifier(context, Android.Resource.Drawable.SymDefAppIcon);
                notifier.SendNotification(title, message);
            }
        }
    
    
        #region Notifier
        public class Notifier
        {
            public int NotificatioId { get; set; } = 1000;
            public string CHANNEL_ID { get; set; } = "location_notification";
            public string Key { get; set; } = "NotificationKey";
            public string Value { get; private set; }
            Context _context;
            private int _drawableIcon;
            public Notifier(Context context, int drawableIcon)
            {
                this._context = context;
                this._drawableIcon = drawableIcon;
            }
            public void SendNotification(string title, string message)
            {
                CreateNotificationChannel(_context);
                // Pass the current button press count value to the next activity:
                Value = title;
                var valuesForActivity = new Bundle();
                valuesForActivity.PutString(Key, Value);
    
                // When the user clicks the notification, Activity will start up.
                var notIntent = new Intent(_context, typeof(MainActivity));
                var contentIntent = PendingIntent.GetActivity(_context, 0, notIntent, PendingIntentFlags.CancelCurrent);
                // Build the notification:
                var builder = new NotificationCompat.Builder(_context, CHANNEL_ID)
                              .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                              .SetContentIntent(contentIntent) // Start up this activity when the user clicks the intent.
                              .SetContentTitle(title) // Set the title
                              .SetSmallIcon(_drawableIcon) // This is the icon to display
                              .SetContentText(message); // the message to display.
                // Finally, publish the notification:
                var notificationManager = NotificationManagerCompat.From(_context);
                notificationManager.Notify(NotificatioId, builder.Build());
                // Increment the button press count:
            }
            void CreateNotificationChannel(Context context)
            {
                if (Build.VERSION.SdkInt < BuildVersionCodes.O)
                {
                    // Notification channels are new in API 26 (and not a part of the
                    // support library). There is no need to create a notification
                    // channel on older versions of Android.
                    return;
                }
                var name = "channel_name";
                var description = "channel_description";
                var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)
                {
                    Description = description
                };
                channel.SetSound(null, null);
                var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
                notificationManager.CreateNotificationChannel(channel);
            }
            #endregion
        }
    }
    

    Just register once

    LocalNotificationService service = new LocalNotificationService();
                service.LocalNotification(233, DateTime.Now.AddSeconds(5));
    
Sign In or Register to comment.