Migration to Android SDK v.5.x.x on Xamarin

How to migrate to Android SDK v.5.x.x in a few steps

PushManager

PushManager consisted of various methods for different features. These methods are now split between different classes:

  • PushNotificationsManager

  • PushwooshNotificationSettings

  • PushwooshInApp

  • PushwooshBadge

  • PushwooshLocation

BaseRegistrationReceiver

BaseRegistrationReceiver was used to handle push registration and unregistration events. This receiver is now replaced by a callback mechanism:

using Pushwoosh;
using Pushwoosh.Exception;
using Android.Runtime;
using Pushwoosh.Function;

class RegistrationListener : Java.Lang.Object, ICallback
{
    public void Process(Result result)
    {
        if (result.IsSuccess) {
          string token = (string)result.Data;
            // handle successful registration
        } else {
          PushwooshException exception = result.Exception.JavaCast<PushwooshException>();
          // handle registration error
        }
    }
}

PushNotificationsManager.Instance.RegisterForPushNotifications(new RegistrationListener());

BasePushMessageReceiver

BasePushMessageReceiver was used to imitate iOS notification behavior when receiving push notification in the foreground. It was done by cancelling incoming notification and invoking OnMessageReceive callback. Such workaround required manual registration when the app became active, and unregistration when the app went into the background.

This receiver has been replaced by NotificationServiceExtension:

using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Annotation;
using Pushwoosh.Notification;

[assembly: MetaData("com.pushwoosh.notification_service_extension", Value = "com.your.package.YourNotificationServiceExtension")]
namespace YourNamespace
{
    [Register("com/your/package/YourNotificationServiceExtension")]
    public class YourNotificationServiceExtension : NotificationServiceExtension
    {
        protected override bool OnMessageReceived(PushMessage message)
        {
            if (IsAppOnForeground)
            {
                Handler mainHandler = new Handler(ApplicationContext.MainLooper);
                mainHandler.Post(() =>
                {
                    HandlePush(message);
                });
                return true;
            }
            return false;
        }

        protected override void StartActivityForPushMessage(PushMessage message)
        {
            base.StartActivityForPushMessage(message);
            HandlePush(message);
        }

        [MainThread]
        void HandlePush(PushMessage message)
        {
            //TODO: handle push message
        }
    }
}

This extension is also used to handle notification arrival and tap events, thus replacing all clumsy code that was used in manual Activity integration.

PushFragment

PushFragment was a lightweight alternative for the complex integration involving Activity lifecycle. On the other hand, it required FragmentActivity inheritance and implicitly used more complex Fragment lifecycle.

PushFragment and PushEventListener are now replaced by Pushwoosh#registerForPushNotifications(Callback) and NotificationServiceExtension.

Custom Push Broadcast Receiver (PW_NOTIFICATION_RECEIVER)

PW_NOTIFICATION_RECEIVER was used to customize the app's behavior when user tapped the notification. This allowed to handle notifications outside of Activity context and to open different Activities depending on notification content. Such integration used internal Pushwoosh SDK API which no longer exists.

This receiver is now completely replaced by NotificationServiceExtension:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Pushwoosh.Notification;

[assembly: MetaData("com.pushwoosh.notification_service_extension", 
           Value = "com.your.package.YourNotificationServiceExtension")]
namespace YourNamespace
{
    [Register("com/your/package/YourNotificationServiceExtension")]
    public class YourNotificationServiceExtension : NotificationServiceExtension
    {
        protected override void StartActivityForPushMessage(PushMessage message)
        {
            // base.StartActivityForPushMessage(message) starts default launcher activity 
            // or activity marked with ${applicationId}.MESSAGE action.
            // Simply do not call it to override this behaviour.

            // start your activity instead:
            Intent launchIntent = new Intent(ApplicationContext, typeof(YourActivity));
            launchIntent.SetFlags(ActivityFlags.NewTask);

            ApplicationContext.StartActivity(launchIntent);
        }
    }
}

Notification Factory

There also were some breaking changes involving notification factory:

1. AbsNotificationFactory was replaced by NotificationFactory 2. AbsNotificationFactory#onPushReceived(PushData) and AbsNotificationFactory#onPushHandle(Activity) methods replaced by NotificationServiceExtension class (onMessageReceived, startActivityForPushMessage). 3. DefaultNotificationFactory was replaced by PushwooshNotificationFactory. 4. PushData was replaced by PushMessage.

In-App Messages

1. InAppFacade was replaced by PushwooshInApp. 2. pushwoosh object was inroduced for JavaScript native interface with following API: getHwid(): string - returns Pushwoosh HWID of the device. getVersion(): string - returns current Pushwoosh SDK version. postEvent(event: string, attributes?: object, successCallback?: function, errorCallback?: function) - sends postEvent request. sendTags(tags: object) - sends tags associated with the device. getTags(successCallback: function, errorCallback?: function) - returns tags associated with the device. closeInApp() - closes In-App html page.

Share your feedback with us

Your feedback helps us create a better experience, so we would love to hear from you if you have any issues during the SDK integration process. If you face any difficulties, please do not hesitate to share your thoughts with us via this form.

Last updated