How do I get PendingIntent using the MvvmCross 5 IMvxNavigationService?
Asked Answered
T

1

6

I have a method I used in MvvmCross 4.x that was used with the NotificationCompat.Builder to set a PendingIntent of a notification to display a ViewModel when the notification is clicked by the user. I'm trying to convert this method to use the MvvmCross 5.x IMvxNavigationService but can't see how to setup the presentation parameters, and get a PendingIntent using the new navigation API.

private PendingIntent RouteNotificationViewModelPendingIntent(int controlNumber, RouteNotificationContext notificationContext, string stopType)
{
    var request = MvxViewModelRequest<RouteNotificationViewModel>.GetDefaultRequest();
    request.ParameterValues = new Dictionary<string, string>
    {
        { "controlNumber", controlNumber.ToString() },
        { "notificationContext", notificationContext.ToString() },
        { "stopType", stopType }
    };
    var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
    var intent = translator.GetIntentFor(request);
    intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

    return PendingIntent.GetActivity(Application.Context,
                                     _notificationId,
                                     intent,
                                     PendingIntentFlags.UpdateCurrent);
}

The RouteNotificationViewModel does appear when I click the notification but Prepare and Initialize are not being called. What is necessary to convert this method from MvvmCross 4.x style of navigation to MvvmCross 5.x style of navigation?

Titration answered 5/10, 2017 at 21:29 Comment(4)
Can you add the code you're using to receive the intent?Anselmi
There is no code receiving the intent. Android creates the activity when I click on the notification. That part is working, but Prepare and Initialize do not get called by MvvmCross when the activity is shown by Android. This all worked in MvvmCross 4 assuming the navigation parameters were manually serialized. Now in MvvmCross 5 the navigation parameters are serialized differently.Titration
Can you open an issue in the repo for this? This scenario is not currently supported for v5 Navigation I'm afraid. But we're working actively on the MvxNavigationService.Anselmi
Here's the GitHub issue for anyone following this.Titration
T
0

It's possible to do this in MvvmCross 5+ but it's not as clean as it previously was.

For starters you want to specify the singleTop launch mode for your activity:

[Activity(LaunchMode = LaunchMode.SingleTop, ...)]
public class MainActivity : MvxAppCompatActivity

Generate the notification PendingIntent like this:

var intent = new Intent(Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
// Putting an extra in the Intent to pass data to the MainActivity
intent.PutExtra("from_notification", true);
var pendingIntent = PendingIntent.GetActivity(Context, notificationId, intent, 0);

Now there are two places to handle this Intent from MainActivity while still allowing the use of MvvmCross navigation service:

If the app was not running while the notification was clicked then OnCreate will be called.

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    if (bundle == null && Intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was not running. 
        // Calling MvxNavigationService multiple times in a row here won't always work as expected. Use a Task.Delay(), Handler.Post(), or even an MvvmCross custom presentation hint to make it work as needed.
    }
}

If the app was running while the notification was clicked then OnNewIntent will be called.

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    if (intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was already running.
        // Back stack is already setup.
        // Show a new fragment using MvxNavigationService.
    }
}
Titration answered 25/4, 2019 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.