Mvvmcross Android Notification Pass Parameter to Existing ViewModel

401 views Asked by At

I have a foreground service with a notification. When the user clicks the notification, it will bring the user back to the main screen viewmodel (or load it again if unloaded).

I want to add 2 actions (pause, stop) to the notification. It is preferred to call the same viewmodel with a parameter to indicate the action type, and let the main screen viewmodel to handle the action. As the main screen viewmodel may have been loaded already, no more initialization will be executed. Passing parameter like showing viewmodel does not work. The existing viewmodel does not know it was triggered from the notification indeed.

How can I pass a different parameter for each action type to the viewmodel, and retrieve it in the viewmodel to act accordingly? Or it should be done in a different way?

This is the code of the notification:

                var request = MvxViewModelRequest<RouteLayoutViewModel>.GetDefaultRequest();
                var intent = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>().GetIntentFor(request);
                const int pendingIntentId = 0;
                PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, pendingIntentId, intent, PendingIntentFlags.UpdateCurrent);

                var builder = new NotificationCompat.Builder(this);

                builder
                    .SetContentTitle(AppConstants.AppName)
                    .SetContentText("notification_text")
                    .SetSmallIcon(Resource.Drawable.Icon)
                    .SetContentIntent(pendingIntent); ..........

                var notification = builder.Build();

                StartForeground(AppConstants.SERVICE_RUNNING_NOTIFICATION_ID, notification);

Thanks,

Nick

1

There are 1 answers

2
Trevor Balcom On BEST ANSWER

MvvmCross 5 changed the way the ViewModel parameters are serialized internally. MvvmCross 5 hasn't been updated yet to handle this scenario AFAIK. Here is some sample code demonstrating how to workaround the issue using a BroadcastReceiver currently. This approach will allow you to navigate with parameters after the user clicks on the Android Notification.

When you're building the notification:

var intent = new Intent(context, typeof(YourBroadcastReceiver));
intent.SetAction("YOUR_ACTION");
// Put your other parameters here...
intent.PutExtra("id", id);
var pendingIntent = PendingIntent.GetBroadcast(context, _notificationId, intent, PendingIntentFlags.UpdateCurrent);
...
notificationBuilder.SetContentIntent(pendingIntent);

Then you add a BroadcastReceiver class like so:

[BroadcastReceiver]
public class YourBroadcastReceiver : MvxBroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == "YOUR_ACTION")
        {
            // TODO: Extract the contents from the intent.
            var id = intent.GetIntExtra("id", 0);
            // TODO: Navigate using IMvxNavigationService using the parameters pulled from the Intent.
        }
    }
}