I attempted to follow the instructions found here to add a MessagingCenter subscribe action on a notification tap that would open a specific view. Somewhere my Send / Subscribe aren't talking to each other and I just can't see where. The details of the messaging center are still new to me so I'm sure I just used the wrong class or sender somewhere.
The code below has since been modified from what was shown to me in the link. The idea is still roughly the same though.
Here's the SendLocalNotification method in my FirebaseService class:
void SendLocalNotification(string body)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.PutExtra("OpenPage", "SomePage");
//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)
.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());
}
Here's the OnNewIntent method in the android MainActivity:
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("OpenPage"))
{
MessagingCenter.Send(this, "Notification");
}
base.OnNewIntent(intent);
}
And here is where I attempt to subscribe to the message in my LoadsPageViewModel (not in the android project):
public LoadsPageViewModel()
{
MessagingCenter.Subscribe<LoadsPageViewModel>(this, "Notification", (sender) =>
{
// navigate to a view here
});
}