Having trouble using MessagingCenter to send a message from Android MainActivity to view model class not in Android project

半腔热情 提交于 2021-02-11 16:58:46

问题


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
        });
    }

回答1:


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.-



来源:https://stackoverflow.com/questions/61283306/having-trouble-using-messagingcenter-to-send-a-message-from-android-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!