I have a push notification with 2 actions (Accept, Decline). How can I determine which action was actually tapped and then have the notification disappear (acting as if the user has tapped the actual notification and not the action button)? It looks like both action taps go into my OnNewIntent method and I'd like to do different actions based on which one was tapped. My notification is also still showing in the notification bar except the actions (buttons) are no longer displayed. The notification fully goes away when I tap it.
Here's my SendLocalNotification method in FirebaseService (ignore the comments. they're for me):
void SendLocalNotification(string body)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.PutExtra("OpenPage", "SomePage");
//intent.PutExtra("message", body);
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
var pendingIntent = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
.AddAction(0, "Decline", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
.SetContentTitle("Load Match")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText(body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
And then my OnNewIntent in MainActivity:
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("OpenPage"))
{
MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");
}
base.OnNewIntent(intent);
}
EDIT: My OnNewIntent has that logic currently because I was using the MessagingCenter to send a message to a subscriber on a view model and doing a view navigation. Then I discovered Actions and that's how I ended up here trying to do that logic with the notification buttons.