Exemplo n.º 1
0
        public void Test()
        {
            Random random            = new Random();
            int    randomNumber      = random.Next(9999 - 1000) + 1000;
            var    localNotification = new LocalNotification
            {
                Title      = "Rescue",
                Body       = "Sending emergency",
                Id         = 0,
                NotifyTime = DateTime.Now,
                IconId     = Resource.Drawable.ic_dialog_close_light
            };
            Intent newIntent = new Intent(Android.App.Application.Context, typeof(MainActivity));

            Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Android.App.Application.Context);
            stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
            stackBuilder.AddNextIntent(newIntent);
            PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            var builder = new Notification.Builder(Android.App.Application.Context)
                          .SetContentTitle(localNotification.Title)
                          .SetContentText(localNotification.Body)
                          .SetSmallIcon(localNotification.IconId)
                          .SetContentIntent(resultPendingIntent)
                          .SetAutoCancel(true);
            var notificationManager = NotificationManager.FromContext(Android.App.Application.Context);

            notificationManager.Notify(randomNumber, builder.Build());
        }
Exemplo n.º 2
0
        //private NotificationManager notificationManager;

        #region ILocalNotifications implementation

        public void SendLocalNotification(string title, string description, long time)
        {
            var builder = new Notification.Builder(Application.Context)
                          .SetContentTitle(title)
                          .SetContentText(description)
                          .SetWhen(time)
                          .SetDefaults(NotificationDefaults.All)
                          .SetSmallIcon(Resource.Drawable.icon);
            var notification = builder.Build();
            var resultIntent = new Intent(Application.Context, typeof(AfazerService));

            resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
            var stackBuilder = TaskStackBuilder.Create(Application.Context);

            stackBuilder.AddNextIntent(resultIntent);
            var resultPendingIntent =
                stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            builder.SetContentIntent(resultPendingIntent);

            var notificationManager =
                Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
            const int notificationId = 0;

            notificationManager?.Notify(notificationId, notification);
        }
Exemplo n.º 3
0
        public override void OnReceive(Context context, Intent intent)
        {
            var resultIntent = new Intent(context, typeof(MainActivity));

            resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

            var stackBuilder = TaskScheduler.Create(context);

            stackBuilder.AddParentStack(Class.FromType(typeof(MainActivity)));
            stackBuilder.AddNextIntent(resultIntent);

            var chk = new ChecRunning();

            if (!chk.isMyServiceRunning(typeof(BatteryService)))
            {
                Toast.MakeText(context, "Service Restarted!", ToastLength.Long).Show();
                var backgroundServiceIntent = new Intent(context, typeof(BatteryService));
                context.StartForegroundService(backgroundServiceIntent);
            }
            else
            {
                Toast.MakeText(context, "alarm tick!", ToastLength.Long).Show();
            }


            //if (!initiate)
            //{
            //    BatteryBroadcastReceiver mReceiver = new BatteryBroadcastReceiver();
            //    context.RegisterReceiver(mReceiver, new IntentFilter(Intent.ActionBatteryChanged));
            //}
        }
        public void ShowNotification(string title, string content)
        {
            CreatNotificationChannel();
            Context context     = Android.App.Application.Context;
            var     intent      = getLaucherActivity();
            var     stackBuiler = TaskStackBuilder.Create(context);

            stackBuiler.AddNextIntent(intent);
            var pendingIntent = stackBuiler.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            if (builder == null)
            {
                builder = new Notification.Builder(context, "channel_ID");
            }

            builder.SetAutoCancel(true);
            builder.SetContentTitle(title);
            builder.SetContentIntent(pendingIntent);
            builder.SetContentText(content);
            //

            builder.SetSmallIcon(Resource.Drawable.ic_vol_type_tv_dark);
            // Resource.Drawable.ic_stat_button_click;

            Notification notification = builder.Build();

            NotificationManager notificationManager =
                context.GetSystemService(Context.NotificationService) as NotificationManager;

            notificationManager.Notify(NOTIFICATION_ID, notification);
        }
Exemplo n.º 5
0
        // Handler for button click events.
        private void ButtonOnClick(object sender, EventArgs eventArgs)
        {
            // Pass the current button press count value to the next activity:
            Bundle valuesForActivity = new Bundle();

            valuesForActivity.PutInt("count", count);

            // When the user clicks the notification, SecondActivity will start up.
            Intent resultIntent = new Intent(this, typeof(SecondActivity));

            // Pass some values to SecondActivity:
            resultIntent.PutExtras(valuesForActivity);

            // Construct a back stack for cross-task navigation:
            TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);

            stackBuilder.AddParentStack(Class.FromType(typeof(SecondActivity)));
            stackBuilder.AddNextIntent(resultIntent);

            // Create the PendingIntent with the back stack:
            PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            // Build the notification:
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                                                 .SetAutoCancel(true)                                                             // Dismiss the notification from the notification area when the user clicks on it
                                                 .SetContentIntent(resultPendingIntent)                                           // Start up this activity when the user clicks the intent.
                                                 .SetContentTitle("Button Clicked")                                               // Set the title
                                                 .SetNumber(count)                                                                // Display the count in the Content Info
                                                 .SetSmallIcon(Resource.Drawable.ic_stat_button_click)                            // This is the icon to display
                                                 .SetContentText(String.Format("The button has been clicked {0} times.", count)); // the message to display.

            // Finally, publish the notification:
            NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);

            notificationManager.Notify(ButtonClickNotificationId, builder.Build());

            // Increment the button press count:
            count++;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Notification builder
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        /// <param name="notificationID"></param>
        /// <returns></returns>
        private Android.Support.V4.App.NotificationCompat.Builder publishnotification_(string title, string text, int notificationID)
        {
            // Generate notification
            Intent notificationIntent = new Intent(ActivityContext.mActivity, typeof(MainActivity));

            Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(ActivityContext.mActivity);
            stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
            stackBuilder.AddNextIntent(notificationIntent);

            PendingIntent contentIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            // Instantiate the builder and set notification elements, including
            // the pending intent:
            Android.Support.V4.App.NotificationCompat.Builder builder = new Android.Support.V4.App.NotificationCompat.Builder(ActivityContext.mActivity, CHANNEL_ID)
                                                                        .SetContentTitle(title)
                                                                        .SetContentText(text)
                                                                        .SetSmallIcon(Resource.Drawable.Icon)
                                                                        .SetAutoCancel(true)
                                                                        .SetContentIntent(contentIntent);

            // Build the notification:
            Notification notification = builder.Build();

            // Get the notification manager:
            NotificationManager notificationManager =
                ActivityContext.mActivity.GetSystemService(Context.NotificationService) as NotificationManager;

            // Publish the notification:
            notificationManager.Notify(notificationID, notification);

#if DEBUG
            Console.WriteLine("Notification published: " + notificationID);
#endif

            Log.println("Notification published: " + notificationID);

            return(builder);
        }