Xamarin Forms - Processing a Notification Click

2020-04-06 16:21发布

问题:

I have a Xamarin Forms application which raises an Android Notification but I'm having trouble creating a simple page that will interact with the user when they click the Notification. I understand that in Xamarin.Forms there is only 1 activity and so the pending Intent must be to that mainActivity

I have set the LaunchMode to SingleTop and and Intent Filter to match the Intent name used in the pendingIntent

Now when I click the Notification I do get routed to the OnResume of the MainActivity but I don't understand how to: 1) Recognise that I am in this activity because of the notification click - I tried adding an Extra to the pending Intent but it is not there when I inspect this.Intent.Extras 2) Even if I know that I'm in the activity due to the notification click, how do I launch a specific page from the Activity. I'm new to Xamarin but I can't see how to navigate to a Content Page or access the Navigation Stack.

This must be a really common use case but I can't find anything relevant.

回答1:

Ensure the you have set LaunchMode.SingleTop on your MainActivity:

LaunchMode.SingleTop

[Activity(~~~, LaunchMode = LaunchMode.SingleTop, ~~~]
public class MainActivity
{
   ~~~~

In your MainActivity (the FormsAppCompatActivity subclass) add a OnNewIntent override:

OnNewIntent:

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    NotificationClickedOn(intent);
}

Now you can check the intent.Action / intent.HasExtra to determine if it is your notification that was send and thus process it. With Xamarin.Forms the easiest would be to use MessagingCenter to send a message that is subscribed to within your .NetStd/PCL Xamarin.Forms code base.

NotificationClickedOn:

void NotificationClickedOn(Intent intent)
{
    if (intent.Action == "ASushiNotification" && intent.HasExtra("MessageFromSushiHangover"))
    {
        /// Do something now that you know the user clicked on the notification...

        var notificationMessage = intent.Extras.GetString("MessageFromSushiHangover");
        var winnerToast = Toast.MakeText(this, $"{notificationMessage}.\n\n