1

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
        });
    }
user1818298
  • 509
  • 2
  • 8
  • 23

1 Answers1

11

For the MessagingCenter to work, you need to use the same type/object on the sender and the subscriber.

Since you're sending from the Android Project, the this value you are using here:

MessagingCenter.Send(this, "Notification");

represents the MainActivity.

And when you are subscribing in your ViewModel you are using the ViewModel object

MessagingCenter.Subscribe<LoadsPageViewModel>(this, "Notification", (sender) => { });

This is the reason why you are not receiving the Message on the other side.

To make it work you will need to change the following:

In the Android Main Activity, use the Xamarin.Forms.Application Class:

MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");

And in your ViewModel use the same Xamarin.Forms.Application class and object:

MessagingCenter.Subscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current, "Notification", (sender) =>
{
    Console.WriteLine("Received Notification...");
});

That way you will comply with what MessagagingCenter is expecting.

Hope this helps.-

pinedax
  • 9,246
  • 2
  • 23
  • 30
  • 1
    This looked to be exactly what I needed and you presented it with a little explanation behind what everything was doing. Fantastic answer! – user1818298 Apr 18 '20 at 13:22
  • Sorry to bother you with a follow up question @pinedax but I just tried to move the MC.Subscribe logic to a different view model (I actually want to redirect to a different view than what I had originally posted) and for whatever reason the Subscribe isn't being hit. It works perfectly in the view model I have posted in the original question. Do you have any thoughts on why it works for one view model but not for another? – user1818298 Apr 18 '20 at 21:08
  • @user1818298 did you solve your issue when moved the messaging to another ViewModel? – pinedax Apr 26 '20 at 18:23
  • 1
    i did. im not sure what is causing it but when i move the message subscribe to another view model it works perfectly so i just moved forward with that approach :) – user1818298 Apr 26 '20 at 22:26