コード例 #1
0
ファイル: Method.cs プロジェクト: kevinoskee/Rescue
        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());
        }
コード例 #2
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++;
        }
コード例 #3
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);
        }