Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Update an activity for a local notification

HSDRHSDR USMember ✭✭

Hi,

I am creating a local notification and adding an activity using this code which creates a button that says: "Start Work":

PendingIntent startWorkIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.UpdateCurrent); builder.AddAction(Resource.Drawable.workGreen, "Start Work", startWorkIntent);

My question is once I have sent the message to the system using:

notification = builder.Build(); manager.Notify(messageId, notification);

how can I modify the text and the text on the button?

Currently I am canceling the notification using manager.CancelAll(); and then creating a new notification but that doesn't seem very elegant.

Also looking at the documentation for AddAction (https://docs.microsoft.com/en-us/dotnet/api/android.app.notification.builder.addaction?view=xamarin-android-sdk-9#applies-to) it states:

Warning
This API is now obsolete.

What should we be using instead of AddAction?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    how can I modify the text and the text on the button?

    If you want to change the text for the action at the runtime, you have to cancle the notification, then re-create an new notification. Becuase NotificationCompat.Builder do not have method to set previous Action.

    Normally, we update the Notification by notificationManager.Notify(NOTIFICATION_ID, builder.Build()); with same NOTIFICATION_ID and same NotificationCompat.Builder,like this thread,

    https://stackoverflow.com/a/32561752/10627299

    AddAction This API is now obsolete.

    You can use following code for notification action.

       // Create the PendingIntent with the back stack:            
                var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int) PendingIntentFlags.UpdateCurrent);
              var  action = new NotificationCompat.Action.Builder(Resource.Drawable.workGreen, "Start Work", resultPendingIntent).Build();
    

    Then set the action by AddAction(action).

      builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                              .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
                              .AddAction(action)
                              .SetNumber(count) // Display the count in the Content Info
                              .SetSmallIcon(Resource.Drawable.ic_stat_button_click) // This is the icon to display
                              .SetContentText($"The button has been clicked {count} times."); // the message to display.
    
Sign In or Register to comment.