public override Task IssueNotificationAsync(string title, string message, string id, bool alertUser, Protocol protocol, int?badgeNumber, NotificationUserResponseAction userResponseAction, string userResponseMessage) { if (_notificationManager == null) { return(Task.CompletedTask); } else if (message == null) { CancelNotification(id); } else { Intent notificationIntent = new Intent(Application.Context, typeof(AndroidMainActivity)); notificationIntent.PutExtra(NOTIFICATION_USER_RESPONSE_ACTION_KEY, userResponseAction.ToString()); notificationIntent.PutExtra(NOTIFICATION_USER_RESPONSE_MESSAGE_KEY, userResponseMessage); PendingIntent notificationPendingIntent = PendingIntent.GetActivity(Application.Context, 0, notificationIntent, PendingIntentFlags.OneShot); SensusNotificationChannel notificationChannel = SensusNotificationChannel.Default; if (userResponseAction == NotificationUserResponseAction.DisplayPendingSurveys) { notificationChannel = SensusNotificationChannel.Survey; } // reset channel to silent if we're not notifying/alerting or if we're in an exclusion window if (!alertUser || (protocol != null && protocol.TimeIsWithinAlertExclusionWindow(DateTime.Now.TimeOfDay))) { notificationChannel = SensusNotificationChannel.Silent; } Notification.Builder notificationBuilder = CreateNotificationBuilder(notificationChannel) .SetContentTitle(title) .SetContentText(message) .SetSmallIcon(Resource.Drawable.ic_launcher) .SetContentIntent(notificationPendingIntent) .SetAutoCancel(true) .SetOngoing(false); if (badgeNumber != null) { notificationBuilder.SetNumber(badgeNumber.Value); } // use big-text style for long messages if (message.Length > 20) { Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle(); bigTextStyle.BigText(message); notificationBuilder.SetStyle(bigTextStyle); } _notificationManager.Notify(id, 0, notificationBuilder.Build()); } return(Task.CompletedTask); }
void ShowNotification(bool custom) { Resources res = Resources; NotificationManager notificationManager = (NotificationManager)GetSystemService( NotificationService); Notification.Builder builder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.ic_stat_notify_example) .SetAutoCancel(true) .SetTicker(GetString(Resource.String.notification_text)) .SetContentIntent(GetDialogPendingIntent("Tapped the notification entry.")); if (custom) { // Sets a custom content view for the notification, including an image button. RemoteViews layout = new RemoteViews(PackageName, Resource.Layout.notification); layout.SetTextViewText(Resource.Id.notification_title, GetString(Resource.String.app_name)); layout.SetOnClickPendingIntent(Resource.Id.notification_button, GetDialogPendingIntent("Tapped the 'dialog' button in the notification.")); builder.SetContent(layout); // Notifications in Android 3.0 now have a standard mechanism for displaying large // bitmaps such as contact avatars. Here, we load an example image and resize it to the // appropriate size for large bitmaps in notifications. Bitmap largeIconTemp = BitmapFactory.DecodeResource(res, Resource.Drawable.notification_default_largeicon); Bitmap largeIcon = Bitmap.CreateScaledBitmap( largeIconTemp, res.GetDimensionPixelSize(Android.Resource.Dimension.NotificationLargeIconWidth), res.GetDimensionPixelSize(Android.Resource.Dimension.NotificationLargeIconHeight), false); largeIconTemp.Recycle(); builder.SetLargeIcon(largeIcon); } else { builder .SetNumber(7) // An example number. .SetContentTitle(GetString(Resource.String.app_name)) .SetContentText(GetString(Resource.String.notification_text)); } notificationManager.Notify(NotificationDefault, builder.Notification); }
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { //Getting Notification Service mManager = (NotificationManager)this.BaseContext.ApplicationContext .GetSystemService(NotificationService); /* * When the user taps the notification we have to show the Home Screen * of our App, this job can be done with the help of the following * Intent. */ Intent intent1 = new Intent(this.BaseContext.ApplicationContext, typeof(MainActivity)); intent1.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop); PendingIntent pendingNotificationIntent = PendingIntent.GetActivity( Android.App.Application.Context.ApplicationContext, 0, intent1, PendingIntentFlags.UpdateCurrent); Notification.Builder builder = new Notification.Builder(this); builder.SetAutoCancel(true); //builder.SetTicker("this is ticker text"); builder.SetContentTitle("TimeSheet"); builder.SetContentText("Remember input your timesheet ;)"); builder.SetSmallIcon(Resource.Drawable.logo); builder.SetContentIntent(pendingNotificationIntent); //builder.SetOngoing(true); //setBadgeIconType(1); //builder.SetSubText("This is subtext..."); //API level 16 builder.SetNumber(100); var notification = builder.Build(); mManager.Notify(0, notification); return(base.OnStartCommand(intent, flags, startId)); }
void ShowNotification(bool custom) { Resources res = Resources; NotificationManager notificationManager = (NotificationManager) GetSystemService ( NotificationService); Notification.Builder builder = new Notification.Builder (this) .SetSmallIcon (Resource.Drawable.ic_stat_notify_example) .SetAutoCancel (true) .SetTicker (GetString(Resource.String.notification_text)) .SetContentIntent (GetDialogPendingIntent ("Tapped the notification entry.")); if (custom) { // Sets a custom content view for the notification, including an image button. RemoteViews layout = new RemoteViews (PackageName, Resource.Layout.notification); layout.SetTextViewText (Resource.Id.notification_title, GetString (Resource.String.app_name)); layout.SetOnClickPendingIntent (Resource.Id.notification_button, GetDialogPendingIntent ("Tapped the 'dialog' button in the notification.")); builder.SetContent (layout); // Notifications in Android 3.0 now have a standard mechanism for displaying large // bitmaps such as contact avatars. Here, we load an example image and resize it to the // appropriate size for large bitmaps in notifications. Bitmap largeIconTemp = BitmapFactory.DecodeResource (res, Resource.Drawable.notification_default_largeicon); Bitmap largeIcon = Bitmap.CreateScaledBitmap ( largeIconTemp, res.GetDimensionPixelSize (Android.Resource.Dimension.NotificationLargeIconWidth), res.GetDimensionPixelSize (Android.Resource.Dimension.NotificationLargeIconHeight), false); largeIconTemp.Recycle (); builder.SetLargeIcon (largeIcon); } else { builder .SetNumber (7) // An example number. .SetContentTitle (GetString (Resource.String.app_name)) .SetContentText (GetString (Resource.String.notification_text)); } notificationManager.Notify (NotificationDefault, builder.Notification); }