How to get PendingIntent using MvvmCross 5 IMvxNavigationService?

I have a method that I used in MvvmCross 4.x, which was used with NotificationCompat.Builder to set PendingIntent notifications to display the ViewModel when the user clicks on the notification. I am trying to convert this method to use MvvmCross 5.x IMvxNavigationService but I can’t figure out how to configure presentation settings and get 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 a notification, but Prepare and Initialize not called. What is needed to convert this method from the MvvmCross 4.x navigation style to the MvvmCross 5.x navigation style?

+5
source share
1 answer

This can be done in MvvmCross 5+, but it is not as clean as before.

First, you want to specify the SingleTop startup mode for your activity:

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

Create a PendingIntent notification as follows:

 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 ways to handle this intention from MainActivity, while at the same time allowing you to use the MvvmCross navigation service:

If the application was not launched while clicking on the notification, OnCreate will be.

 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 application was launched while clicking on the notification, OnNewIntent will be.

 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. } } 
0
source

Source: https://habr.com/ru/post/1272374/


All Articles