Skip to content

Latest commit

 

History

History
420 lines (297 loc) · 49.1 KB

MIGRATION_GUIDE.md

File metadata and controls

420 lines (297 loc) · 49.1 KB

React Native v5.0.0 Migration Guide

⚠️ Migration Advisory for current OneSignal customers

Our new user-centric APIs and v5.x.x SDKs offer an improved user and data management experience. However, they may not be at 1:1 feature parity with our previous versions yet.

If you are migrating an existing app, we suggest using iOS and Android’s Phased Rollout capabilities to ensure that there are no unexpected issues or edge cases. Here is the documentation for each:

If you run into any challenges or have concerns, please contact our support team at support@onesignal.com

Intro

In this release, we are making a significant shift from a device-centered model to a user-centered model. A user-centered model allows for more powerful omni-channel integrations within the OneSignal platform.

To facilitate this change, the externalId approach for identifying users is being replaced by the login and logout methods. In addition, the SDK now makes use of namespaces such as User, Notifications, and InAppMessages to better separate code.

This migration guide will walk you through the React Native SDK changes as a result of this shift.

Overview

Under the user-centered model, the concept of a "player" is being replaced with three new concepts: users, subscriptions, and aliases.

Users

A user is a new concept which is meant to represent your end-user. A user has zero or more subscriptions and can be uniquely identified by one or more aliases. In addition to subscriptions, a user can have data tags which allows for user attribution.

Subscription

A subscription refers to the method in which an end-user can receive various communications sent by OneSignal, including push notifications, SMS, and email. In previous versions of the OneSignal platform, each of these channels was referred to as a “player”. A subscription is in fact identical to the legacy “player” concept. Each subscription has a subscription_id (previously, player_id) to uniquely identify that communication channel.

Aliases

Aliases are a concept evolved from external user ids which allows the unique identification of a user within a OneSignal application. Aliases are a key-value pair made up of an alias label (the key) and an alias id (the value). The alias label can be thought of as a consistent keyword across all users, while the alias id is a value specific to each user for that particular label. The combined alias label and alias id provide uniqueness to successfully identify a user.

OneSignal uses a built-in alias label called external_id which supports existing use of external user ids. external_id is also used as the identification method when a user identifies themselves to the OneSignal SDK via OneSignal.login. Multiple aliases can be created for each user to allow for your own application's unique identifier as well as identifiers from other integrated applications.

Migration Guide (v4 to v5)

The React Native SDK accesses the OneSignal native iOS and Android SDKs. For this update, all SDK versions are aligning across OneSignal’s suite of client SDKs. As such, the native iOS SDK is making the jump from v3 to v5. See existing install instructions here for more information.

iOS

Notification Service Extension Changes

In your Project Root > ios > Podfile, update the notification service extension:

    // 4.x.x
    target 'OneSignalNotificationServiceExtension' do
      pod 'OneSignalXCFramework', '>= 3.0', '< 4.0'
    end

    // 5.x.x
    target 'OneSignalNotificationServiceExtension' do
      pod 'OneSignalXCFramework', '>= 5.0', '< 6.0'
    end

Close Xcode. While still in the ios directory, run pod install --repo-update.

API Changes

Namespaces

The OneSignal SDK has been updated to be more modular in nature. The SDK has been split into namespaces, and functionality previously in the static OneSignal class has been moved to the appropriate namespace. The namespaces and how to access them in code are as follows:

Namespace Access Pattern
Debug OneSignal.Debug
InAppMessages OneSignal.InAppMessages
LiveActivities OneSignal.LiveActivities
Location OneSignal.Location
Notifications OneSignal.Notifications
Session OneSignal.Session
User OneSignal.User

Initialization

Initialization of the OneSignal SDK is now completed through the initialize method. A typical initialization now looks similar to below.

Navigate to your index.ts file, or the first Javascript file that loads with your app.

Replace the following:

OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');

To the match the new initialization:

OneSignal.initialize('YOUR_ONESIGNAL_APP_ID');

for iOS: Remove any usages of setLaunchURLsInApp as the method and functionality has been removed.

If your integration is not user-centric, there is no additional startup code required. A device-scoped user (please see definition of “device-scoped user” below in Glossary) is automatically created as part of the push subscription creation, both of which are only accessible from the current device or through the OneSignal dashboard.

If your integration is user-centric, or you want the ability to identify the user beyond the current device, the login method should be called to identify the user:

OneSignal.login('USER_EXTERNAL_ID');

The login method will associate the device’s push subscription to the user that can be identified via the alias externalId=USER_EXTERNAL_ID. If that user doesn’t already exist, it will be created. If the user does already exist, the user will be updated to own the device’s push subscription. Note that the push subscription for the device will always be transferred to the newly logged in user, as that user is the current owner of that push subscription.

Once (or if) the user is no longer identifiable in your app (i.e. they logged out), the logout method should be called:

OneSignal.logout();

Logging out has the affect of reverting to a device-scoped user, which is the new owner of the device’s push subscription.

Subscriptions

In previous versions of the SDK, a “player” could have up to one email address and up to one phone number for SMS. In the user-centered model, a user can own the current device’s Push Subscription along with the ability to have zero or more email subscriptions and zero or more SMS subscriptions. Note: If a new user logs in via the login method, the previous user will no longer own that push subscription.

Push Subscription

The current device’s push subscription can be retrieved via:

const id: string = OneSignal.User.pushSubscription.getPushSubscriptionId();
const token: string = OneSignal.User.pushSubscription.getPushSubscriptionToken();
const optedIn: boolean = OneSignal.User.pushSubscription.getOptedIn();

Opting In and Out of Push Notifications

To receive push notifications on the device, call the push subscription’s optIn() method. If needed, this method will prompt the user for push notifications permission.

Note: For greater control over prompting for push notification permission, you may use the OneSignal.Notifications.requestPermission method detailed below in the API Reference.

OneSignal.User.pushSubscription.optIn();

If at any point you want the user to stop receiving push notifications on the current device (regardless of system-level permission status), you can use the push subscription to opt out:

OneSignal.User.pushSubscription.optOut();

To resume receiving of push notifications (driving the native permission prompt if permissions are not available), you can opt back in with the optIn method from above.

Email/SMS Subscriptions

Email and/or SMS subscriptions can be added or removed via the following methods. The remove methods will result in a no-op if the specified email or SMS number does not exist on the user within the SDK, and no request will be made.

// Add email subscription
OneSignal.User.addEmail('customer@company.com');
// Remove previously added email subscription
OneSignal.User.removeEmail('customer@company.com');

// Add SMS subscription
OneSignal.User.addSms('+15558675309');
// Remove previously added SMS subscription
OneSignal.User.removeSms('+15558675309');

API Reference

Below is a comprehensive reference to the 5.0.0 OneSignal React Native SDK.

OneSignal

The SDK is still accessible via a OneSignal static class. It provides access to higher level functionality and is a gateway to each subspace of the SDK.

React Native Description
OneSignal.initialize("YOUR_ONESIGNAL_APP_ID") Initializes the OneSignal SDK. This should be called during startup of the application.
OneSignal.login("USER_EXTERNAL_ID") Login to OneSignal under the user identified by the [externalId] provided. The act of logging a user into the OneSignal SDK will switch the [user] context to that specific user.

- If the [externalId] exists, the user will be retrieved and the context will be set from that user information. If operations have already been performed under a device-scoped user, they will not be applied to the now logged in user (they will be lost).
- If the [externalId] does not exist the user, the user will be created and the context set from the current local state. If operations have already been performed under a device-scoped user, those operations will be applied to the newly created user.

Push Notifications and In App Messaging
Logging in a new user will automatically transfer the push notification and in app messaging subscription from the current user (if there is one) to the newly logged in user. This is because both push notifications and in-app messages are owned by the device.
OneSignal.logout() Logout the user previously logged in via [login]. The [user] property now references a new device-scoped user. A device-scoped user has no user identity that can later be retrieved, except through this device as long as the app remains installed and the app data is not cleared.
OneSignal.setConsentGiven(true) Indicates whether privacy consent has been granted. This field is only relevant when the application has opted into data privacy protections. See [requiresPrivacyConsent].
OneSignal.setConsentRequired(true) Determines whether a user must consent to privacy prior to their user data being sent up to OneSignal. This should be set to true prior to the invocation of initialize to ensure compliance.

Live Activities Namespace

Live Activities are a type of interactive push notification. Apple introduced them in October 2022 to enable iOS apps to provide real-time updates to their users that are visible from the lock screen and the dynamic island.

Please refer to OneSignal’s guide on Live Activities, the Live Activities Quickstart tutorial, and the existing SDK reference on Live Activities.

React Native Description
OneSignal.LiveActivities.enter("ACTIVITY_ID", "TOKEN")

See below for usage of callbacks
Entering a Live Activity associates an activityId with a live activity temporary push token on OneSignal's server. The activityId is then used with the OneSignal REST API to update one or multiple Live Activities at one time.
OneSignal.LiveActivities.exit("ACTIVITY_ID")

See below for usage of callbacks
Exiting a Live activity deletes the association between a customer defined activityId with a Live Activity temporary push token on OneSignal's server.
// Enter a Live Activity
OneSignal.LiveActivities.enter('ACTIVITY_ID', 'TOKEN', (results) => {
  console.log('Results of entering live activity');
  console.log(results);
});

// Exit a Live Activity
OneSignal.LiveActivities.exit('ACTIVITY_ID', (results) => {
  console.log('Results of exiting live activity');
  console.log(results);
});

User Namespace

The User namespace is accessible via OneSignal.User and provides access to user-scoped functionality.

React Native Description
OneSignal.User.setLanguage("en") Set the 2-character language for this user.
OneSignal.User.addAlias("ALIAS_LABEL", "ALIAS_ID") Set an alias for the current user. If this alias label already exists on this user, it will be overwritten with the new alias id.
OneSignal.User.addAliases({ALIAS_LABEL_01: "ALIAS_ID_01", ALIAS_LABEL_02: "ALIAS_ID_02"}) Set aliases for the current user. If any alias already exists, it will be overwritten to the new values.
OneSignal.User.removeAlias("ALIAS_LABEL") Remove an alias from the current user.
OneSignal.User.removeAliases(["ALIAS_LABEL_01", "ALIAS_LABEL_02"]]) Remove aliases from the current user.
OneSignal.User.addEmail("customer@company.com") Add a new email subscription to the current user.
OneSignal.User.removeEmail("customer@company.com") Results in a no-op if the specified email does not exist on the user within the SDK, and no request will be made.
OneSignal.User.addSms("+15558675309") Add a new SMS subscription to the current user.
OneSignal.User.removeSms("+15558675309") Results in a no-op if the specified phone number does not exist on the user within the SDK, and no request will be made..
OneSignal.User.addTag("KEY", "VALUE") Add a tag for the current user. Tags are key:value pairs used as building blocks for targeting specific users and/or personalizing messages. If the tag key already exists, it will be replaced with the value provided here.
OneSignal.User.addTags({"KEY_01": "VALUE_01", "KEY_02": "VALUE_02"}) Add multiple tags for the current user. Tags are key:value pairs used as building blocks for targeting specific users and/or personalizing messages. If the tag key already exists, it will be replaced with the value provided here.
OneSignal.User.removeTag("KEY") Remove the data tag with the provided key from the current user.
OneSignal.User.removeTags(["KEY_01", "KEY_02"]) Remove multiple tags with the provided keys from the current user.
OneSignal.User.getTags() Returns the local tags for the current user.
OneSignal.User.addEventListener("change", (event: UserChangedState) => void)

See below for usage
Add a User State callback which contains the nullable onesignalId and externalId. The listener will be fired when these values change.
await OneSignal.User.getOnesignalId() Returns the OneSignal ID for the current user, which can be null if it is not yet available.
await OneSignal.User.getExternalId() Returns the External ID for the current user, which can be null if not set.

User State Listener

    const listener = (event: UserChangedState) => {
        console.log("User changed: " + (event));
    };

    OneSignal.User.addEventListener("change", listener);
    // Remove the listener
    OneSignal.User.removeEventListener("change", listener);

Push Subscription Namespace

The Push Subscription namespace is accessible via OneSignal.User.pushSubscription and provides access to push subscription-scoped functionality.

React Native Description
OneSignal.User.pushSubscription.getPushSubscriptionId() DEPRECATED
use getIdAsync.
OneSignal.User.pushSubscription.getPushSubscriptionToken() DEPRECATED
use getTokenAsync
OneSignal.User.pushSubscription.getOptedIn() DEPRECATED
use getOptedInAsync
await OneSignal.User.pushSubscription.getIdAsync() The readonly push subscription ID.
await OneSignal.User.pushSubscription.getTokenAsync() The readonly push token.
await OneSignal.User.pushSubscription.getOptedInAsync() Gets a boolean value indicating whether the current user is opted in to push notifications. This returns true when the app has notifications permission and optedOut is called. Note: Does not take into account the existence of the subscription ID and push token. This boolean may return true but push notifications may still not be received by the user.
OneSignal.User.pushSubscription.optIn() Call this method to receive push notifications on the device or to resume receiving of push notifications after calling optOut. If needed, this method will prompt the user for push notifications permission.
OneSignal.User.pushSubscription.optOut() If at any point you want the user to stop receiving push notifications on the current device (regardless of system-level permission status), you can call this method to opt out.
OneSignal.User.pushSubscription.addEventListener('change', listener: (event) => void)

See below for usage
Adds the listener to run when the push subscription changes.
OneSignal.User.pushSubscription.removeEventListener('change', listener)

See below for usage
Remove a push subscription listener that has been previously added.

Push Subscription Observer

// Create an observer
OneSignal.User.pushSubscription.addEventListener('change', (subscription) => {
  console.log('OneSignal: subscription changed:', subscription);
});

// Removes the previously added observer
OneSignal.User.pushSubscription.removeEventListener('change', subscription);

Session Namespace

The Session namespace is accessible via OneSignal.Session and provides access to session-scoped functionality.

React Native Description
OneSignal.Session.addOutcome("OUTCOME_NAME") Add an outcome with the provided name, captured against the current session.
OneSignal.Session.addUniqueOutcome("OUTCOME_NAME") Add a unique outcome with the provided name, captured against the current session.
OneSignal.Session.addOutcomeWithValue("OUTCOME_NAME", 1) Add an outcome with the provided name and value, captured against the current session.

Notifications Namespace

The Notifications namespace is accessible via OneSignal.Notifications and provides access to notification-scoped functionality.

React Native Description
OneSignal.Notifications.hasPermission() DEPRECATED
use getPermissionAsync()
OneSignal.Notifications.getPermissionAsync() Whether this app has push notification permission.
await OneSignal.Notifications.canRequestPermission() Whether attempting to request notification permission will show a prompt. Returns true if the device has not been prompted for push notification permission already.
await OneSignal.Notifications.permissionNative() (ios only) Returns the enum for the native permission of the device. It will be one of: NotDetermined, Denied, Authorized, Provisional (only available in iOS 12), Ephemeral (only available in iOS 14)
OneSignal.Notifications.clearAll(); Removes all OneSignal notifications.
OneSignal.Notifications.removeNotification(32432) (Android only) Cancels a single OneSignal notification based on its Android notification integer ID. Use instead of Android's [android.app.NotificationManager.cancel], otherwise the notification will be restored when your app is restarted.
OneSignal.Notifications.removeGroupedNotifications("GROUP_KEY") (Android only) Cancels a group of OneSignal notifications with the provided group key. Grouping notifications is a OneSignal concept, there is no [android.app.NotificationManager] equivalent.
await OneSignal.Notifications.requestPermission(fallbackToSettings: boolean)

See below for usage
Prompt the user for permission to receive push notifications. This will display the native system prompt to request push notification permission.
OneSignal.Notifications.registerForProvisionalAuthorization() (iOS only) Instead of having to prompt the user for permission to send them push notifications, your app can request provisional authorization.
OneSignal.Notifications.addEventListener("permissionChange", (observer) => {});

See below for usage
This method will fire when a notification permission setting changes. This happens when the user enables or disables notifications for your app from the system settings outside of your app.
OneSignal.Notifications.removeEventListener("permissionChange", (observer) => {});

See below for usage
Remove a push permission observer that has been previously added.
OneSignal.Notifications.addEventListener("foregroundWillDisplay", (event) => {};)

See below for usage
Sets the handler to run before displaying a notification while the app is in focus. Use this handler to read notification data and change it or decide if the notification should show or not.

Note: this runs after the Notification Service Extension which can be used to modify the notification before showing it.
OneSignal.Notifications.addEventListener("click", (event) => {};)

See below for usage
Sets a handler that will run whenever a notification is opened by the user.

Prompt for Push Notification Permission

OneSignal.Notifications.requestPermission(true).then(accepted => {
  console.log('User accepted notifications: ' + accepted);
});

Permission Observer

Add an observer when permission status changes. You can call removeEventListener to remove any existing listeners.

// Add an observer
OneSignal.Notifications.addEventListener('permissionChange', (granted: boolean) => {
  console.log('OneSignal: permission changed:', granted);
});

// Remove previously added observer
OneSignal.Notifications.removeEventListener('permissionChange', observer);

Notification Lifecycle Listener

OneSignal.Notifications.addEventListener('foregroundWillDisplay', (event: NotificationWillDisplayEvent) => {
  event.preventDefault();
  // some async work

  // Use display() to display the notification after some async work
  event.getNotification().display();
});

Notification Click Listener

OneSignal.Notifications.addEventListener('click', (event: NotificationClickEvent) => {
  console.log('OneSignal: notification clicked: ' + event);
});

Location Namespace

The Location namespace is accessible via OneSignal.Location and provide access to location-scoped functionality.

React Native Description
OneSignal.Location.setShared(shared: boolean) Set whether location is currently shared with OneSignal.
await OneSignal.Location.isShared() Whether location is currently shared with OneSignal.
OneSignal.Location.requestPermission() Use this method to manually prompt the user for location permissions. This allows for geotagging so you send notifications to users based on location.

InAppMessages Namespace

The In App Messages namespace is accessible via OneSignal.InAppMessages and provide access to in app messages-scoped functionality.

React Native Description
await OneSignal.InAppMessages.getPaused()

OneSignal.InAppMessages.setPaused(true)
Whether in-app messaging is currently paused. When set to true, no IAM will be presented to the user regardless of whether they qualify for them. When set to false, any IAMs the user qualifies for will be presented to the user at the appropriate time.
OneSignal.InAppMessages.addTrigger("triggerKey", "triggerValue") Add a trigger for the current user. Triggers are currently explicitly used to determine whether a specific IAM should be displayed to the user. See Triggers.

If the trigger key already exists, it will be replaced with the value provided here. Note that triggers are not persisted to the backend. They only exist on the local device and are applicable to the current user.
OneSignal.InAppMessages.addTriggers({"triggerKey1":"triggerValue", "triggerKey2": "triggerValue"}) Add multiple triggers for the current user. Triggers are currently explicitly used to determine whether a specific IAM should be displayed to the user. See Triggers.

If any trigger key already exists, it will be replaced with the value provided here. Note that triggers are not persisted to the backend. They only exist on the local device and are applicable to the current user.
OneSignal.InAppMessages.removeTrigger("triggerKey") Remove the trigger with the provided key from the current user.
OneSignal.InAppMessages.removeTriggers(["triggerKey1", "triggerKey2"]) Remove multiple triggers from the current user.
OneSignal.InAppMessages.clearTriggers() Clear all triggers from the current user.
OneSignal.InAppMessages.setLifecycleHandler(handlerObject)

See below for usage
Set the in-app message lifecycle handler.
OneSignal.InAppMessages.setClickHandler(handler)

See below for usage
Set the in-app message click handler.

In-App Message Click Listener

OneSignal.InAppMessages.addEventListener('click', (event) => {
  console.log('OneSignal IAM clicked: ' + event);
});

In-App Message Lifecycle Listeners

OneSignal.InAppMessages.addEventListener('willDisplay', (event) => {
  console.log('OneSignal: will display IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('didDisplay', (event) => {
  console.log('OneSignal: did display IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('willDismiss', (event) => {
  console.log('OneSignal: will dismiss IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('didDismiss', (event) => {
  console.log('OneSignal: did dismiss IAM: ', event);
});

Debug Namespace

The Debug namespace is accessible via OneSignal.Debug and provide access to debug-scoped functionality.

React Native Description
OneSignal.Debug.setLogLevel(LogLevel.Verbose) Sets the log level the OneSignal SDK should be writing to the Xcode log.
OneSignal.Debug.setAlertLevel(LogLevel.Verbose) Sets the logging level to show as alert dialogs.

Glossary

device-scoped user

An anonymous user with no aliases that cannot be retrieved except through the current device or OneSignal dashboard. On app install, the OneSignal SDK is initialized with a device-scoped user. A device-scoped user can be upgraded to an identified user by calling OneSignal.login("USER_EXTERNAL_ID") to identify the user by the specified external user ID.

Limitations

  • Changing app IDs is not supported.
  • Any User namespace calls must be invoked after initialization. Example: OneSignal.User.addTag("tag", "2")
  • In the SDK, the user state is only refreshed from the server when a new session is started (cold start or backgrounded for over 30 seconds) or when the user is logged in. This is by design.

Known issues

  • Identity Verification
    • We will be introducing Identity Verification using JWT in a follow up release