Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate the MvvmCross Routing plugin with my existing push notification service


How do I integrate the MvvmCross Routing plugin with my existing push notification service


Integrating the MvvmCross Routing plugin with an existing push notification service involves several steps. This integration allows you to handle deep linking from notifications, directing users to specific parts of your app based on the notification content.

Step 1: Install the MvvmCross Routing Plugin

First, you need to install the MvvmCross Routing plugin in your project. This plugin is not a standard MvvmCross plugin but rather a community-developed solution for deep linking. You can find it on GitHub under the MvvmCross-Routing repository. Since it's not available as a NuGet package, you might need to clone the repository and include it manually in your project.

Step 2: Configure Routing Attributes

To use the routing plugin, you need to decorate your ViewModels with routing attributes. These attributes define the URI patterns that will trigger navigation to specific ViewModels. Here's an example of how you might decorate a ViewModel:

csharp
[assembly: MvxRouting(typeof(ViewModelA), @"mvx://test/\?id=(?[A-Z0-9]{32})$")]
namespace MyProject.ViewModels
{
    public class ViewModelA : MvxViewModel
    {
        public void Init(string id)
        {
            // Handle the id parameter
        }
    }
}

Step 3: Implement Push Notification Handling

To integrate push notifications with routing, you need to handle the notification intent or URL scheme in your app. On Android, you can use `Intent` to navigate to a specific ViewModel when a notification is clicked. On iOS, you can handle the URL scheme in the `AppDelegate`.

For Android, you can generate an `Intent` using `IMvxAndroidViewModelRequestTranslator` to navigate to a ViewModel:

csharp
var translator = Mvx.Resolve();
var request = MvxViewModelRequest.GetDefaultRequest(typeof(ViewModelA));
var intent = translator.GetIntentFor(request);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent);

For iOS, you need to handle the URL scheme in `AppDelegate.OpenUrl`:

csharp
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
    if (url.Scheme == "mvx")
    {
        // Handle the mvx scheme by routing to the appropriate ViewModel
        var routingService = Mvx.Resolve();
        routingService.RouteAsync(url.AbsoluteString);
        return true;
    }
    return false;
}

Step 4: Register Routing Service

Ensure that the routing service is registered in your IoC container. This is typically done in the `Load` method of a plugin or during app setup:

csharp
public class RoutingPlugin : IMvxPlugin
{
    public void Load()
    {
        Mvx.IocProvider.RegisterSingleton(() => new MvxRoutingService());
    }
}

Step 5: Test Deep Linking

Finally, test that deep linking works correctly from notifications. You can simulate this by creating a notification with a custom intent or URL scheme that matches your routing configuration.

By following these steps, you can integrate the MvvmCross Routing plugin with your push notification service, enabling seamless navigation to specific parts of your app when notifications are clicked.

Citations:
[1] https://www.mvvmcross.com/documentation/plugins/getting-started
[2] https://github.com/charri/MvvmCross-Routing
[3] https://stackoverflow.com/questions/18402448/using-notifications-on-android-with-mvvmcross
[4] https://stackoverflow.com/questions/73128508/how-to-load-plugins-when-updating-to-mvvmcross-6-4-2-from-5-6-2
[5] https://www.mvvmcross.com/documentation/plugins/plugins-development
[6] https://docs.expo.dev/guides/using-push-notifications-services
[7] https://www.mvvmcross.com/mvvmcross-5-release/
[8] https://www.mvvmcross.com/documentation/getting-started/getting-started
[9] https://docs.amplify.aws/gen1/swift/build-a-backend/push-notifications/set-up-push-notifications/
[10] https://www.mvvmcross.com/mvvmcross-6.0.0-release/