Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide an example of using the Pushwoosh plugin for MvvmCross


Can you provide an example of using the Pushwoosh plugin for MvvmCross


To use the Pushwoosh plugin for MvvmCross, you need to integrate it into your cross-platform application. Here's a step-by-step guide on how to do it:

Step 1: Install the Pushwoosh Plugin

First, you need to install the Pushwoosh plugin for MvvmCross. This can be done via NuGet. Open your project in Visual Studio, right-click on your project in the Solution Explorer, select "Manage NuGet Packages," and search for "Pushwoosh plugin for MvvmCross." Install it for both Android and iOS projects.

Step 2: Configure Pushwoosh in Your App

After installing the plugin, you need to configure it in your application. This involves setting up the Pushwoosh SDK in your Android and iOS projects.

Android Configuration

For Android, you need to initialize the Pushwoosh SDK in your `MainActivity`. Here's a basic example:

csharp
using Pushwoosh;

public class MainActivity : MvxActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Initialize Pushwoosh
        PushwooshManager pushManager = PushwooshManager.GetInstance(this);
        pushManager.OnStartup(this);
    }
}

iOS Configuration

For iOS, you need to initialize the Pushwoosh SDK in your `AppDelegate`. Here's how you can do it:

csharp
using Pushwoosh;

[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate, App>
{
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // Initialize Pushwoosh
        PushwooshManager pushManager = PushwooshManager.GetInstance();
        pushManager.OnStartup(application, launchOptions);

        return base.FinishedLaunching(application, launchOptions);
    }
}

Step 3: Register for Push Notifications

To receive push notifications, you need to register your device with Pushwoosh. This is typically done in the `OnCreate` method for Android or `FinishedLaunching` for iOS.

Android Registration

csharp
// In MainActivity
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Register for push notifications
    PushwooshManager pushManager = PushwooshManager.GetInstance(this);
    pushManager.RegisterForPushNotifications();
}

iOS Registration

csharp
// In AppDelegate
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    // Register for push notifications
    PushwooshManager pushManager = PushwooshManager.GetInstance();
    pushManager.RegisterForPushNotifications(application);

    return base.FinishedLaunching(application, launchOptions);
}

Step 4: Handle Push Notifications

To handle incoming push notifications, you need to implement a callback method. This method will be called whenever a push notification is received.

Android Handling

csharp
// In MainActivity
protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);

    // Handle push notification intent
    PushwooshManager pushManager = PushwooshManager.GetInstance(this);
    pushManager.OnNewIntent(intent);
}

iOS Handling

csharp
// In AppDelegate
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler)
{
    // Handle push notification
    PushwooshManager pushManager = PushwooshManager.GetInstance();
    pushManager.OnDidReceiveRemoteNotification(application, userInfo, completionHandler);
}

Step 5: Integrate with MvvmCross

To integrate Pushwoosh with MvvmCross, you can create a service that handles push notifications and inject it into your view models. Here's a basic example of how you might structure this service:

csharp
public interface IPushService
{
    void RegisterForPushNotifications();
    void HandlePushNotification(Dictionary data);
}

public class PushService : IPushService
{
    public void RegisterForPushNotifications()
    {
        // Use PushwooshManager to register for notifications
    }

    public void HandlePushNotification(Dictionary data)
    {
        // Handle incoming push notification data
    }
}

Then, register this service in your MvvmCross `App` class:

csharp
public class App : MvxApplication
{
    public override void Initialize()
    {
        base.Initialize();

        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        // Register PushService manually if needed
        Mvx.IoCProvider.RegisterSingleton(() => new PushService());
    }
}

Finally, inject this service into your view models to use Pushwoosh functionality:

csharp
public class MyViewModel : MvxViewModel
{
    private readonly IPushService _pushService;

    public MyViewModel(IPushService pushService)
    {
        _pushService = pushService;
    }

    // Use _pushService to register for or handle notifications
}

This setup allows you to manage push notifications across different platforms using the Pushwoosh plugin with MvvmCross.

Citations:
[1] https://www.mvvmcross.com/documentation/plugins/3rd-party-plugins
[2] https://visualstudiomagazine.com/articles/2018/05/08/build-a-xamarin.forms-application.aspx
[3] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[4] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/
[5] https://github.com/MvvmCross/MvvmCross-Samples
[6] https://docs.pushwoosh.com/product/audience-data-and-segmentation/user-data-tags/tags/
[7] https://stackoverflow.com/questions/19443456/mvvmcross-messenger-plugin-sample-applications
[8] https://github.com/MvvmCross/MvvmCross/issues/1415