0

In the example below, the mvvmcross-based android app is opened from a Notification/PendingIntent. The PendingIntent's target is actually an MvxFragment.

When the Notification is tapped, nothing happens in the app, the associated ViewModel's constructor isn't fired and no MVX events are logged. If the PendingIntent's target is changed to being an Activity derivative, everything works correctly.

What then is the correct 'MVX' way to handle scenarios like this, ie Notification's target is a fragment.

Sample broken code:

        var appContext = Mvx.Resolve<IMvxAndroidGlobals>().ApplicationContext;

        // SomeViewModel  --- derives MvxViewModel
        // SomeViewModelView --- front end MvxFragment for SomeViewModel 
        var request = new MvxViewModelRequest<SomeViewModel>(
            new MvxBundle(SomeViewModel.CreateParameters("a_parameter_value").ToSimplePropertyDictionary()),
            null,
            null);

        var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
        var uiIntent = translator.GetIntentFor(request);
        var pendingUiIntent = PendingIntent.GetActivity(appContext, 0, uiIntent, 0);

        var notificationManager = (NotificationManager)appContext.GetSystemService(Context.NotificationService);

        var notificationBuilder = new NotificationCompat.Builder(context)
            .SetAutoCancel(true)
        ...
            .SetContentIntent(onSelectedIntent);

        // show the notification
        notificationManager.Notify(id, notificationBuilder.Build());


        // after user taps notification, nothing happens
geoffreys
  • 1,107
  • 7
  • 24

1 Answers1

0

I'd guess your code is failing somewhere in the GetIntentFor stage - you can't simply request a fragment Intent - for navigation Intents only really make sense for Activitys. My guess is that your code is actually failing with an exception thrown from https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxAndroidViewsContainer.cs#L127

What then is the correct 'MVX' way to handle scenarios like this, ie Notification's target is a fragment.

There isn't a standard answer to your question, either in MvvmCross or in vanilla Android.

But you can't just send an Intent to show a Fragment - Android doesn't work like that.

You'll need to work out what your app needs to do in response to this notification - does it need to switch to a new Activity to host the Fragment or does it need to show the Fragment in an existing Activity. Once you've worked that out then you may be able to work out what MvvmCross ViewModel you actually want to show and/or what actions you want your code to take.

Looking around there are quite a few Java questions on here with similar questions - they may help: - Launch a fragment in my Android application from the notification bar - Fragments and Notifications: Target different Activities from Notification; depending on screen configuration

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165