# Migration guide

### Migration from Android SDK 5 to 6

1. Migrate your project to AndroidX according to the [official documentation](https://developer.android.com/jetpack/androidx/migrate).
2. Add a new module in the app's **build.gradle** file if you are using **Firebase Cloud Messaging.**

```groovy title="build.gradle"
implementation 'com.pushwoosh:pushwoosh-firebase:+'
```

For those who use **Amazon**, and do not use **Firebase**, no changes needed.

### Migration from previous versions of Android SDK

Most of Pushwoosh classes and methods became deprecated since 5.0 release and some of them became extinct. **PushManager**, **BasePushMessageReceiver**, **BaseRegistrationReceiver**, **SendPushTagsCallback**, **PushFragment** and **PushEventListener** classes are still available as part of `com.pushwoosh:pushwoosh-deprecated` library but it is recommended to migrate to new API as soon as possible.

## PushManager

**PushManager** consisted of various methods for different features. These methods are now split between different classes and libraries:\
`com.pushwoosh:pushwoosh`: **Pushwoosh**, **PushwooshNotificationSettings**, **PushwooshInApp**.\
`com.pushwoosh:pushwoosh-badge`: **PushwooshBadge**.\
`com.pushwoosh:pushwoosh-location`: **PushwooshLocation**.\
`com.pushwoosh:pushwoosh-beacon`: **PushwooshBeacon**.

## BaseRegistrationReceiver

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

```java
Pushwoosh.getInstance().registerForPushNotifications(result -> {
    if (result.isSuccess()) {
        String token = result.getData();
        // handle successful registration
    }
    else {
        PushwooshException exception = result.getException();
        // handle registration error
    }
});
```

## BasePushMessageReceiver

**BasePushMessageReceiver** was used to imitate iOS notification behaviour when receiving push notification in foreground. It was achieved by cancelling incoming notification and invoking **onMessageReceive** callback. It was clunky and required manual registration when application becomes active and deregistration when application goes background.\
This receiver was replaced by **NotificationServiceExtension**:

```txt title="AndroidManifest.xml"
<meta-data
    android:name="com.pushwoosh.notification_service_extension"
    android:value="com.your.package.name.YourNotificationServiceExtension"/>
```

```java title="YourNotificationServiceExtension.java"
public class YourNotificationServiceExtension extends NotificationServiceExtension {
    @Override
    public boolean onMessageReceived(final PushMessage message) {
        if (isAppOnForeground()) {
            Handler mainHandler = new Handler(getApplicationContext().getMainLooper());
            mainHandler.post(() -> {
                handlePush(message);
            });

            // this indicates that notification should not be displayed
            return true;
        }

        return false;
    }

    @Override
    protected void startActivityForPushMessage(PushMessage message) {
        super.startActivityForPushMessage(message);
        handlePush(message);
    }

    @MainThread
    private void handlePush(PushMessage message) {
        // TODO: handle push message
    }
}
```

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

## PushFragment

**PushFragment** was a lightweight alternative for the complex integration involving Activity lifecycle. But 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 customise behaviour when user clicks on notification. This allowed to handle notifications outside of Activity context and to open different Activities depending on notification content. This integration used internal Pushwoosh SDK API which no longer exists.\
This receiver is now completely replaced by **NotificationServiceExtension**:

```java title="YourNotificationServiceExtension.java"
public class YourNotificationServiceExtension extends NotificationServiceExtension {
    @Override
    protected void startActivityForPushMessage(PushMessage message) {
    	// super.startActivityForPushMessage() starts default launcher activity 
    	// or activity marked with ${applicationId}.MESSAGE action.
    	// Simply do not call it to override this behaviour.
        // super.startActivityForPushMessage(message);

        // start your activity instead:
        Intent launchIntent  = new Intent(getApplicationContext(), YourActivity.class);             
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
 
        // (Optional) pass notification data to Activity
        launchIntent.putExtra(Pushwoosh.PUSH_RECEIVE_EVENT, message.toJson().toString());
 
        context.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 introduced for JavaScript native interface with following API:\
**getHwid(): string** - returns pushwoosh hwid for current device.\
**getVersion(): string** - returns current pushwoosh SDK version.\
**postEvent(event: string, attributes?: object, successCallback?: function, errorCallback?: function)** - sends postEvent request.\
**sendTags(tags: object)** - send tags associated with current device.\
**getTags(successCallback: function, errorCallback?: function)** - returns tags associated with current 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](https://docs.google.com/forms/d/e/1FAIpQLSd\_0b8jwn-V\_JmoPLIxIFYbHACCQhrzidOZV3ELywoQPXRSxw/viewform).