To navigate to the correct page after a push notification is clicked using MvvmCross, you need to handle the notification click event and then use the MvvmCross navigation service to move to the desired ViewModel. Here's a step-by-step guide on how to achieve this:
1. Handling Push Notifications
First, ensure your app is set up to receive push notifications. For Android, you can use Firebase Cloud Messaging (FCM) as described in the Xamarin documentation[2]. This involves registering your app with FCM and implementing a service to handle token updates.
2. Creating a Notification Intent
When creating a notification, you need to specify an intent that will be triggered when the user clicks on the notification. This intent should launch your app's main activity.
csharp
// Example of creating a notification intent
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
3. Handling Notification Click
In your main activity, you need to check if the app was launched from a notification. You can do this by overriding the `OnNewIntent` method in your main activity:
csharp
public class MainActivity : MvxActivity
{
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
// Check if the intent contains data related to the notification
if (intent.HasExtra("notification_data"))
{
// Handle notification data and navigate accordingly
HandleNotificationData(intent);
}
}
private void HandleNotificationData(Intent intent)
{
// Extract notification data
var notificationData = intent.GetStringExtra("notification_data");
// Use MvvmCross navigation service to navigate to the correct ViewModel
var navigationService = Mvx.IoCProvider.Resolve();
if (navigationService.CanNavigate())
{
navigationService.Navigate();
}
}
}
4. Providing a Hint for AppStart
If your app needs to handle push notifications when it's not running, you might need to provide a hint to the `AppStart` method. This can be done by overriding the `GetAppStartHint` method in your custom `AppStart` class:
csharp
public class CustomAppStart : MvxAppStart
{
public override object GetAppStartHint(object hint = null)
{
if (hint is Intent intent && intent.HasExtra("notification_data"))
{
return intent.GetStringExtra("notification_data");
}
return base.GetAppStartHint(hint);
}
protected override void Startup(object hint = null)
{
if (hint is string notificationData)
{
// Handle notification data and navigate accordingly
HandleNotificationData(notificationData);
}
else
{
base.Startup(hint);
}
}
private void HandleNotificationData(string notificationData)
{
// Navigate to the correct ViewModel
var navigationService = Mvx.IoCProvider.Resolve();
if (navigationService.CanNavigate())
{
navigationService.Navigate();
}
}
}
5. Using Uri Navigation
Alternatively, you can use MvvmCross's Uri navigation feature to handle deep linking from notifications. This involves defining navigation routes using assembly attributes and then navigating using these routes when the app starts from a notification[4].
csharp
[assembly: MvxNavigation(typeof(MyViewModel), @"mvx://myapp/notification")]
Then, when handling the notification intent, you can navigate using the Uri:
csharp
var navigationService = Mvx.IoCProvider.Resolve();
await navigationService.Navigate("mvx://myapp/notification");
This approach allows for more flexible and platform-independent navigation handling.
Citations:[1] https://stackoverflow.com/questions/23805166/pushnotifications-and-mvvmcross
[2] https://devblogs.microsoft.com/xamarin/implementing-push-notifications-android-apps/
[3] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[4] https://www.mvvmcross.com/documentation/fundamentals/navigation
[5] https://stackoverflow.com/questions/54593528/how-to-flag-if-closed-app-started-from-push-notification-using-mvvmcross-with-cu
[6] https://stackoverflow.com/questions/35488194/mvvmcross-navigate-to-viewmodel-on-android-notification-click
[7] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60
[8] https://github.com/MvvmCross/MvvmCross/issues/2418