diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md
index 05d1fccb..9095189a 100644
--- a/MIGRATION_GUIDE.md
+++ b/MIGRATION_GUIDE.md
@@ -1,4 +1,4 @@
-# React Native v5.0.0-beta-01 Migration Guide
+# React Native v5.0.0-beta-02 Migration Guide
# Intro
@@ -35,14 +35,15 @@ The React Native SDK accesses the OneSignal native iOS and Android SDKs. For thi
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` |
-| Location | `OneSignal.Location` |
-| Notifications | `OneSignal.Notifications` |
-| Session | `OneSignal.Session` |
-| User | `OneSignal.User` |
+| **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
@@ -60,7 +61,7 @@ Replace the following:
To the match the new initialization:
```typescript
- OneSignal.init("YOUR_ONESIGNAL_APP_ID");
+ OneSignal.initialize("YOUR_ONESIGNAL_APP_ID");
```
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.
@@ -130,39 +131,42 @@ Email and/or SMS subscriptions can be added or removed via the following methods
# API Reference
-Below is a comprehensive reference to the `5.0.0-beta-01` OneSignal React Native SDK.
+Below is a comprehensive reference to the `5.0.0-beta-02` 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.
-| | **Description** |
+|**React Native** | **Description** |
|-----------------------------------------------------------------------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `OneSignal.init("YOUR_ONESIGNAL_APP_ID")` | *Initializes the OneSignal SDK. This should be called during startup of the application.* |
+| `OneSignal.initialize("YOUR_ONESIGNAL_APP_ID")` | *Initializes the OneSignal SDK. This should be called during startup of the application.* |
| `OneSignal.User.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.User.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.getPrivacyConsent()`
`OneSignal.setPrivacyConsent(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.setRequiresPrivacyConsent(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.* |
+| `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.* |
| `OneSignal.setLaunchURLsInApp(true)` | *This method can be used to set if launch URLs should be opened in safari or within the application. Set to `true` to launch all notifications with a URL in the app instead of the default web browser. Make sure to call `setLaunchURLsInApp` before the `initialize` call.* |
-| `OneSignal.enterLiveActivity("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.exitLiveActivity("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.* |
-## Live Activities
+
+## 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](https://documentation.onesignal.com/docs/live-activities), the [Live Activities Quickstart](https://documentation.onesignal.com/docs/live-activities-quickstart) tutorial, and the [existing SDK reference](https://documentation.onesignal.com/docs/sdk-reference#live-activities) 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.* |
```typescript
// Enter a Live Activity
- OneSignal.enterLiveActivity("ACTIVITY_ID", "TOKEN", (results) => {
+ OneSignal.LiveActivities.enter("ACTIVITY_ID", "TOKEN", (results) => {
console.log("Results of entering live activity");
console.log(results);
});
// Exit a Live Activity
- OneSignal.exitLiveActivity("ACTIVITY_ID", (results) => {
+ OneSignal.LiveActivities.exit("ACTIVITY_ID", (results) => {
console.log("Results of exiting live activity");
console.log(results);
});
@@ -173,7 +177,7 @@ Please refer to OneSignal’s guide on [Live Activities](https://documentation.o
The User namespace is accessible via `OneSignal.User` and provides access to user-scoped functionality.
-| | **Description** |
+|**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.* |
@@ -196,15 +200,15 @@ The User namespace is accessible via `OneSignal.User` and provides access to use
The Push Subscription namespace is accessible via `OneSignal.User.pushSubscription` and provides access to push subscription-scoped functionality.
-| | **Description** |
+|**React Native** | **Description** |
|------------------------------------------------------------------------------------------------------------------------------------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `await OneSignal.User.PushSubscription.getId()` | *The readonly push subscription ID.* |
| `await OneSignal.User.PushSubscription.getToken()` | *The readonly push token.* |
| `await OneSignal.User.PushSubscription.getOptedIn()` | *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.addChangeHandler(handler: (event: ChangeEvent) => void)`
***See below for usage*** | *The `OSPushSubscriptionObserver.onOSPushSubscriptionChanged` method will be fired on the passed-in object when the push subscription changes. This method returns the current `OSPushSubscriptionState` at the time of adding this observer.* |
-| `OneSignal.User.PushSubscription.removeChangeHandler()`
***See below for usage*** | *Remove a push subscription observer that has been previously added.* |
+| `OneSignal.User.PushSubscription.addObserver(observer: (event) => void)`
***See below for usage*** | *The `OSPushSubscriptionObserver.onOSPushSubscriptionChanged` method will be fired on the passed-in object when the push subscription changes. This method returns the current `OSPushSubscriptionState` at the time of adding this observer.* |
+| `OneSignal.User.PushSubscription.removeObserver(observer)`
***See below for usage*** | *Remove a push subscription observer that has been previously added.* |
### Push Subscription Observer
@@ -213,12 +217,12 @@ Any object implementing the `OSPushSubscriptionObserver` protocol can be added a
```typescript
// Create an observer
-OneSignal.User.PushSubscription.addChangeHandler(subscription => {
+OneSignal.User.PushSubscription.addObserver(subscription => {
console.log('OneSignal: subscription changed: ', subscription);
});
// Removes the previously added observer
-OneSignal.User.PushSubscription.removeChangeHandler();
+OneSignal.User.PushSubscription.removeObserver(subscription);
```
## Session Namespace
@@ -226,7 +230,7 @@ OneSignal.User.PushSubscription.removeChangeHandler();
The Session namespace is accessible via `OneSignal.Session` and provides access to session-scoped functionality.
-| | **Description** |
+|**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.* |
@@ -238,10 +242,10 @@ The Session namespace is accessible via `OneSignal.Session` and provides access
The Notifications namespace is accessible via `OneSignal.Notifications` and provides access to notification-scoped functionality.
-| | **Description** |
+|**React Native** | **Description** |
|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
| `await OneSignal.Notifications.hasPermission()` | *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.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("NOTIFICATION_ID")` | *(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.*|
@@ -249,8 +253,8 @@ The Notifications namespace is accessible via `OneSignal.Notifications` and prov
| `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.addPermissionObserver(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.removePermissionObserver(observer)`
***See below for usage*** | *Remove a push permission observer that has been previously added.*|
-| `OneSignal.Notifications.setNotificationWillShowInForegroundHandler(handler)`
***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](https://documentation.onesignal.com/docs/service-extensions) which can be used to modify the notification before showing it.* |
-| `OneSignal.Notifications.setNotificationOpenedHandler()`
***See below for usage*** | *Sets a handler that will run whenever a notification is opened by the user.*|
+| `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](https://documentation.onesignal.com/docs/service-extensions) 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
@@ -261,37 +265,41 @@ OneSignal.Notifications.requestPermission(accepted => {
```
### Permission Observer
-Add an observer when permission status changes. You can call `removePermissionChangedHandler` to remove any existing listeners.
+Add an observer when permission status changes. You can call `removePermissionObserver` to remove any existing listeners.
```typescript
-// Add a change hanlder
-OneSignal.Notifications.addPermissionChangedHandler(granted => {
- console.log("OneSignal: permission state changed to: ", granted);
-});
-
-// Remove previously added handler
-OneSignal.Notifications.removePermissionChangedHandler();
+// Add an observer
+let observer = function(req) {
+ console.log("OneSignal: permission state changed to: " + (req));
+};
+OneSignal.Notifications.addPermissionObserver(observer);
+
+// Remove previously added observer
+OneSignal.Notifications.removePermissionObserver(observer);
```
-### Notification Will Show in Foreground Handler
+### Notification Lifecycle Listener
```typescript
-OneSignal.Notifications.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
- notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
+OneSignal.Notifications.addEventListener("foregroundWillDisplay", (event) => {
+ event.preventDefault();
+ // some async work
+
+ // Use display() to display the notification after some async work
+ event.getNotification().display();
});
```
-### Notification Click Handler
+### Notification Click Listener
```typescript
-OneSignal.Notifications.setNotificationClickHandler(clicked => {
- const notificationData = JSON.stringify(clicked);
- console.log('notificationOpenedCallback: ' + notificationData);
+OneSignal.Notifications.addEventListener("click", (event) => {
+ console.log('OneSignal: notification clicked: ' + event);
});
```
## Location Namespace
The Location namespace is accessible via `OneSignal.Location` and provide access to location-scoped functionality.
-| | **Description** |
+|**React Native** | **Description** |
|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| `await OneSignal.Location.isShared()`
***See below for usage*** | *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.* |
@@ -322,36 +330,36 @@ const paused = await OneSignal.InAppMessages.getPaused();
console.log("IAM paused: ", paused);
```
-### In-App Message Click Handler
+### In-App Message Click Listener
```typescript
-OneSignal.InAppMessages.setClickHandler(result => {
- const iamClickAction = JSON.stringify(result);
- console.log('iamClickCallback: ' + iamClickAction);
+OneSignal.InAppMessages.addEventListener("click", (event) => {
+ console.log('OneSignal IAM clicked: ' + event);
});
```
-### In-App Message Lifecycle Handler
+### In-App Message Lifecycle Listeners
```typescript
- OneSignal.InAppMessages.setLifecycleHandler({
- onWillDisplayInAppMessage: message => {
- console.log("OneSignal: will display IAM: ", message.messageId)
- },
- onDidDisplayInAppMessage: message => {
- console.log("OneSignal: did display IAM: ", message.messageId)
- },
- onWillDismissInAppMessage: message => {
- console.log("OneSignal: will dismiss IAM: ", message.messageId)
- },
- onDidDismissInAppMessage: message => {
- console.log("OneSignal: did dismiss IAM: ", message.messageId)
- }
- });
+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.
-| **Objective-C** | **Description** |
+| **React Native** | **Description** |
| ---------------------------------------------- | ---------------------------------------------------------------------------------- |
| `OneSignal.Debug.setLogLevel(6)` | *Sets the log level the OneSignal SDK should be writing to the Xcode log.* |
| `OneSignal.Debug.setAlertLevel(0)` | *Sets the logging level to show as alert dialogs.* |
@@ -364,11 +372,12 @@ The Debug namespace is accessible via `OneSignal.Debug` and provide access to de
# Limitations
**General**
- Recommend using only in development and staging environments for Alpha releases
+- Aliases will be available in a future release
+- Outcomes will be available in a future release
- Users are deleted when the last Subscription (push, email, or sms) is removed
- Any `User` namespace calls must be invoked **after** initialization. Example: `OneSignal.User.addTag("tag", "2")`
# Known issues
- Identity Verification
- We will be introducing JWT in a follow-up Beta release
-- User properties may not update correctly when Subscriptions are transferred
- - Please report any issues you find with this
\ No newline at end of file
+
\ No newline at end of file
diff --git a/android/.gradle/4.8/fileChanges/last-build.bin b/android/.gradle/4.8/fileChanges/last-build.bin
new file mode 100644
index 00000000..f76dd238
Binary files /dev/null and b/android/.gradle/4.8/fileChanges/last-build.bin differ
diff --git a/android/.gradle/4.8/fileHashes/fileHashes.bin b/android/.gradle/4.8/fileHashes/fileHashes.bin
new file mode 100644
index 00000000..3b4fa35d
Binary files /dev/null and b/android/.gradle/4.8/fileHashes/fileHashes.bin differ
diff --git a/android/.gradle/4.8/fileHashes/fileHashes.lock b/android/.gradle/4.8/fileHashes/fileHashes.lock
new file mode 100644
index 00000000..3987d30b
Binary files /dev/null and b/android/.gradle/4.8/fileHashes/fileHashes.lock differ
diff --git a/android/.gradle/vcsWorkingDirs/gc.properties b/android/.gradle/vcsWorkingDirs/gc.properties
new file mode 100644
index 00000000..e69de29b
diff --git a/android/.idea/.gitignore b/android/.idea/.gitignore
new file mode 100644
index 00000000..26d33521
--- /dev/null
+++ b/android/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/android/.idea/gradle.xml b/android/.idea/gradle.xml
new file mode 100644
index 00000000..f22a5399
--- /dev/null
+++ b/android/.idea/gradle.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/build.gradle b/android/build.gradle
index 5bbc78a9..1b94c8a3 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -31,7 +31,7 @@ dependencies {
// api is used instead of implementation so the parent :app project can access any of the OneSignal Java
// classes if needed. Such as com.onesignal.NotificationExtenderService
- api 'com.onesignal:OneSignal:5.0.0-beta2'
+ api 'com.onesignal:OneSignal:5.0.0-beta4'
testImplementation 'junit:junit:4.12'
}
\ No newline at end of file
diff --git a/android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java b/android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java
index 34fe7af2..712e28f0 100644
--- a/android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java
+++ b/android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java
@@ -53,28 +53,34 @@ of this software and associated documentation files (the "Software"), to deal
import com.onesignal.OneSignal;
import com.onesignal.debug.LogLevel;
import com.onesignal.inAppMessages.IInAppMessage;
-import com.onesignal.inAppMessages.IInAppMessageClickHandler;
+import com.onesignal.inAppMessages.IInAppMessageClickListener;
+import com.onesignal.inAppMessages.IInAppMessageClickEvent;
import com.onesignal.inAppMessages.IInAppMessageClickResult;
-import com.onesignal.inAppMessages.IInAppMessageLifecycleHandler;
+import com.onesignal.inAppMessages.IInAppMessageLifecycleListener;
+import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent;
+import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent;
+import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent;
+import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
import com.onesignal.notifications.INotification;
-import com.onesignal.notifications.INotificationClickHandler;
-import com.onesignal.notifications.INotificationClickResult;
-import com.onesignal.notifications.INotificationReceivedEvent;
-import com.onesignal.notifications.INotificationWillShowInForegroundHandler;
-import com.onesignal.notifications.IPermissionChangedHandler;
+import com.onesignal.notifications.INotificationClickListener;
+import com.onesignal.notifications.INotificationClickEvent;
+import com.onesignal.notifications.INotificationLifecycleListener;
+import com.onesignal.notifications.INotificationWillDisplayEvent;
+import com.onesignal.notifications.IPermissionObserver;
import com.onesignal.user.subscriptions.IPushSubscription;
-import com.onesignal.user.subscriptions.ISubscription;
-import com.onesignal.user.subscriptions.ISubscriptionChangedHandler;
+import com.onesignal.user.subscriptions.IPushSubscriptionObserver;
+import com.onesignal.user.subscriptions.PushSubscriptionState;
+import com.onesignal.user.subscriptions.PushSubscriptionChangedState;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class RNOneSignal extends ReactContextBaseJavaModule implements
- ISubscriptionChangedHandler,
- IPermissionChangedHandler,
+ IPushSubscriptionObserver,
+ IPermissionObserver,
LifecycleEventListener,
- INotificationWillShowInForegroundHandler{
+ INotificationLifecycleListener{
private ReactApplicationContext mReactApplicationContext;
private ReactContext mReactContext;
@@ -82,27 +88,91 @@ public class RNOneSignal extends ReactContextBaseJavaModule implements
private boolean hasSetPermissionObserver = false;
private boolean hasSetPushSubscriptionObserver = false;
- private HashMap notificationReceivedEventCache;
- private boolean hasSetNotificationWillShowInForegroundHandler = false;
+ private HashMap notificationWillDisplayCache;
+ private HashMap preventDefaultCache;
+
+ private boolean hasAddedNotificationForegroundListener = false;
+ private boolean hasAddedInAppMessageLifecycleListener = false;
+
+ private IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener() {
+ @Override
+ public void onClick(IInAppMessageClickEvent event) {
+ try {
+ IInAppMessageClickResult result = event.getResult();
+ sendEvent("OneSignal-inAppMessageClicked", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageClickResultToMap(result)));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+
+ private IInAppMessageLifecycleListener rnInAppLifecycleListener = new IInAppMessageLifecycleListener() {
+ @Override
+ public void onWillDisplay(IInAppMessageWillDisplayEvent event) {
+ try {
+ sendEvent("OneSignal-inAppMessageWillDisplay", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(event.getMessage())));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void onDidDisplay(IInAppMessageDidDisplayEvent event) {
+ try {
+ sendEvent("OneSignal-inAppMessageDidDisplay", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(event.getMessage())));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void onWillDismiss(IInAppMessageWillDismissEvent event) {
+ try {
+ sendEvent("OneSignal-inAppMessageWillDismiss", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(event.getMessage())));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void onDidDismiss(IInAppMessageDidDismissEvent event) {
+ try {
+ sendEvent("OneSignal-inAppMessageDidDismiss", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(event.getMessage())));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+
+ private INotificationClickListener rnNotificationClickListener = new INotificationClickListener() {
+ @Override
+ public void onClick(INotificationClickEvent event) {
+ try {
+ sendEvent("OneSignal-notificationClicked", RNUtils.convertHashMapToWritableMap(RNUtils.convertNotificationToMap(event.getNotification())));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ };
private void removeObservers() {
- this.removePermissionChangedHandler();
- this.rmeovePushSubscriptionChangeHandler();
+ this.removePermissionObserver();
+ this.removePushSubscriptionObserver();
}
private void removeHandlers() {
- OneSignal.getInAppMessages().setInAppMessageClickHandler(null);
- OneSignal.getInAppMessages().setInAppMessageLifecycleHandler(null);
- OneSignal.getNotifications().setNotificationClickHandler(null);
- OneSignal.getNotifications().setNotificationWillShowInForegroundHandler(null);
+ OneSignal.getInAppMessages().removeClickListener(rnInAppClickListener);
+ OneSignal.getInAppMessages().removeLifecycleListener(rnInAppLifecycleListener);
+ OneSignal.getNotifications().removeClickListener(rnNotificationClickListener);
+ OneSignal.getNotifications().removeForegroundLifecycleListener(this);
}
private void sendEvent(String eventName, Object params) {
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
- private void initNotificationWillShowInForegroundHandlerParams() {
- this.hasSetNotificationWillShowInForegroundHandler = true;
+ private void initNotificationWillDisplayInForegroundListenerParams() {
+ this.hasAddedNotificationForegroundListener = true;
}
public RNOneSignal(ReactApplicationContext reactContext) {
@@ -110,7 +180,8 @@ public RNOneSignal(ReactApplicationContext reactContext) {
mReactApplicationContext = reactContext;
mReactContext = reactContext;
mReactContext.addLifecycleEventListener(this);
- notificationReceivedEventCache = new HashMap();
+ notificationWillDisplayCache = new HashMap();
+ preventDefaultCache = new HashMap();
}
@@ -160,23 +231,13 @@ public void initialize(String appId) {
}
@ReactMethod
- public void setPrivacyConsent(Boolean value) {
- OneSignal.setPrivacyConsent(value);
+ public void setPrivacyConsentGiven(Boolean value) {
+ OneSignal.setConsentGiven(value);
}
@ReactMethod
- public void getPrivacyConsent(Promise promise) {
- promise.resolve(OneSignal.getPrivacyConsent());
- }
-
- @ReactMethod
- public void setRequiresPrivacyConsent(Boolean required) {
- OneSignal.setRequiresPrivacyConsent(required);
- }
-
- @ReactMethod
- public void getRequiresPrivacyConsent(Promise promise) {
- promise.resolve(OneSignal.getRequiresPrivacyConsent());
+ public void setPrivacyConsentRequired(Boolean required) {
+ OneSignal.setConsentRequired(required);
}
@@ -194,58 +255,16 @@ public void setAlertLevel(int logLevel) {
// OneSignal.InAppMessages namespace methods
@ReactMethod
- public void setInAppMessageClickHandler() {
- OneSignal.getInAppMessages().setInAppMessageClickHandler(new IInAppMessageClickHandler() {
- @Override
- public void inAppMessageClicked(IInAppMessageClickResult result) {
- try {
- sendEvent("OneSignal-inAppMessageClicked", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageClickedActionToMap(result)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
+ public void addInAppMessageClickListener() {
+ OneSignal.getInAppMessages().addClickListener(rnInAppClickListener);
}
@ReactMethod
- public void setInAppMessagesLifecycleHandler() {
- OneSignal.getInAppMessages().setInAppMessageLifecycleHandler(new IInAppMessageLifecycleHandler() {
- @Override
- public void onWillDisplayInAppMessage(IInAppMessage message) {
- try {
- sendEvent("OneSignal-inAppMessageWillDisplay", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(message)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public void onDidDisplayInAppMessage(IInAppMessage message) {
- try {
- sendEvent("OneSignal-inAppMessageDidDisplay", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(message)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public void onWillDismissInAppMessage(IInAppMessage message) {
- try {
- sendEvent("OneSignal-inAppMessageWillDismiss", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(message)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public void onDidDismissInAppMessage(IInAppMessage message) {
- try {
- sendEvent("OneSignal-inAppMessageDidDismiss", RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageToMap(message)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
+ public void addInAppMessagesLifecycleListener() {
+ if (!hasAddedInAppMessageLifecycleListener) {
+ OneSignal.getInAppMessages().addLifecycleListener(rnInAppLifecycleListener);
+ hasAddedInAppMessageLifecycleListener = true;
+ }
}
@ReactMethod
@@ -265,7 +284,7 @@ public void addTrigger(String key, String value) {
@ReactMethod
public void addTriggers(ReadableMap triggers) {
- OneSignal.getInAppMessages().addTriggers(triggers.toHashMap());
+ OneSignal.getInAppMessages().addTriggers(RNUtils.convertReadableMapIntoStringMap(triggers));
}
@ReactMethod
@@ -275,7 +294,7 @@ public void removeTrigger(String key) {
@ReactMethod
public void removeTriggers(ReadableArray keys) {
- OneSignal.getInAppMessages().removeTriggers(RNUtils.convertReableArrayIntoStringCollection(keys));
+ OneSignal.getInAppMessages().removeTriggers(RNUtils.convertReadableArrayIntoStringCollection(keys));
}
@ReactMethod
@@ -303,49 +322,40 @@ public void setLocationShared(Boolean shared) {
// OneSignal.Notifications namespace methods
@ReactMethod
- public void setNotificationClickHandler() {
- OneSignal.getNotifications().setNotificationClickHandler(new INotificationClickHandler() {
- @Override
- public void notificationClicked(INotificationClickResult result) {
- try {
- sendEvent("OneSignal-notificationClicked", RNUtils.convertHashMapToWritableMap(RNUtils.convertNotificationClickResultToMap(result)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
+ public void addNotificationClickListener() {
+ OneSignal.getNotifications().addClickListener(rnNotificationClickListener);
}
@ReactMethod
- public void setNotificationWillShowInForegroundHandler() {
- if (this.hasSetNotificationWillShowInForegroundHandler) {
+ public void addNotificationForegroundLifecycleListener() {
+ if (this.hasAddedNotificationForegroundListener) {
return;
}
- OneSignal.getNotifications().setNotificationWillShowInForegroundHandler(this);
- hasSetNotificationWillShowInForegroundHandler = true;
+ OneSignal.getNotifications().addForegroundLifecycleListener(this);
+ hasAddedNotificationForegroundListener = true;
}
@Override
- public void notificationWillShowInForeground(INotificationReceivedEvent notificationReceivedEvent) {
- if (!this.hasSetNotificationWillShowInForegroundHandler) {
- notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
- return;
+ public void onWillDisplay(INotificationWillDisplayEvent event) {
+ if (!this.hasAddedNotificationForegroundListener) {
+ event.getNotification().display();
}
- INotification notification = notificationReceivedEvent.getNotification();
+ INotification notification = event.getNotification();
String notificationId = notification.getNotificationId();
- notificationReceivedEventCache.put(notificationId, notificationReceivedEvent);
+ notificationWillDisplayCache.put(notificationId, (INotificationWillDisplayEvent) event);
+ event.preventDefault();
try {
- sendEvent("OneSignal-notificationWillShowInForeground",
+ sendEvent("OneSignal-notificationWillDisplayInForeground",
RNUtils.convertHashMapToWritableMap(
RNUtils.convertNotificationToMap(notification)));
try {
- synchronized (notificationReceivedEvent) {
- while (notificationReceivedEventCache.containsKey(notificationId)) {
- notificationReceivedEvent.wait();
+ synchronized (event) {
+ while (preventDefaultCache.containsKey(notificationId)) {
+ event.wait();
}
}
} catch(InterruptedException e){
@@ -357,41 +367,44 @@ public void notificationWillShowInForeground(INotificationReceivedEvent notifica
}
@ReactMethod
- public void completeNotificationEvent(final String uuid, final boolean shouldDisplay) {
- INotificationReceivedEvent receivedEvent = notificationReceivedEventCache.get(uuid);
-
- if (receivedEvent == null) {
- Log.e("OneSignal", "(java): could not find cached notification received event with id "+uuid);
+ private void displayNotification(String notificationId){
+ INotificationWillDisplayEvent event = notificationWillDisplayCache.get(notificationId);
+ if (event == null) {
+ Log.e("Could not find onWillDisplayNotification event for notification with id: " + notificationId, null);
return;
}
+ event.getNotification().display();
+ }
- if (shouldDisplay) {
- receivedEvent.complete(receivedEvent.getNotification());
- } else {
- receivedEvent.complete(null);
+ @ReactMethod
+ private void preventDefault(String notificationId) {
+ INotificationWillDisplayEvent event = notificationWillDisplayCache.get(notificationId);
+ if (event == null) {
+ Log.e("Could not find onWillDisplayNotification event for notification with id: " + notificationId, null);
+ return;
}
-
- notificationReceivedEventCache.remove(uuid);
+ event.preventDefault();
+ this.preventDefaultCache.put(notificationId, event);
}
@ReactMethod
- public void addPermissionChangedHandler() {
+ public void addPermissionObserver() {
if (!hasSetPermissionObserver) {
- OneSignal.getNotifications().addPermissionChangedHandler(this);
+ OneSignal.getNotifications().addPermissionObserver(this);
hasSetPermissionObserver = true;
}
}
@ReactMethod
- public void removePermissionChangedHandler() {
+ public void removePermissionObserver() {
if (hasSetPermissionObserver) {
- OneSignal.getNotifications().removePermissionChangedHandler(this);
+ OneSignal.getNotifications().removePermissionObserver(this);
hasSetPermissionObserver = false;
}
}
@Override
- public void onPermissionChanged(boolean permission) {
+ public void onNotificationPermissionChange(boolean permission) {
try {
sendEvent("OneSignal-permissionChanged", RNUtils.convertHashMapToWritableMap(RNUtils.convertPermissionToMap(permission)));
Log.i("OneSignal", "sending permission change event");
@@ -465,37 +478,37 @@ public void optOut() {
}
@ReactMethod
- public void addPushSubscriptionChangeHandler() {
+ public void addPushSubscriptionObserver() {
if (!hasSetPushSubscriptionObserver) {
- OneSignal.getUser().getPushSubscription().addChangeHandler(this);
+ OneSignal.getUser().getPushSubscription().addObserver(this);
hasSetPushSubscriptionObserver = true;
}
}
- @ReactMethod
- public void rmeovePushSubscriptionChangeHandler() {
- if (hasSetPushSubscriptionObserver) {
- OneSignal.getUser().getPushSubscription().removeChangeHandler(this);
- hasSetPushSubscriptionObserver = false;
- }
- }
-
@Override
- public void onSubscriptionChanged(ISubscription subscription) {
- if (!(subscription instanceof IPushSubscription)) {
+ public void onPushSubscriptionChange(PushSubscriptionChangedState pushSubscriptionChangedState) {
+ PushSubscriptionState pushSubscription = pushSubscriptionChangedState.getCurrent();
+ if (!(pushSubscription instanceof PushSubscriptionState)){
return;
}
try {
sendEvent("OneSignal-subscriptionChanged",
RNUtils.convertHashMapToWritableMap(
- RNUtils.convertOnSubscriptionChangedToMap((IPushSubscription) subscription)));
+ RNUtils.convertOnSubscriptionChangedToMap(pushSubscription)));
Log.i("OneSignal", "sending subscription change event");
} catch (JSONException e) {
e.printStackTrace();
- }
+ }
}
+ @ReactMethod
+ public void removePushSubscriptionObserver() {
+ if (hasSetPushSubscriptionObserver) {
+ OneSignal.getUser().getPushSubscription().removeObserver(this);
+ hasSetPushSubscriptionObserver = false;
+ }
+ }
// OneSignal.Session namespace methods
@ReactMethod
@@ -541,12 +554,12 @@ public void removeTag(String key) {
@ReactMethod
public void addTags(ReadableMap tags) {
- OneSignal.getUser().addTags(RNUtils.convertReableMapIntoStringMap(tags));
+ OneSignal.getUser().addTags(RNUtils.convertReadableMapIntoStringMap(tags));
}
@ReactMethod
public void removeTags(ReadableArray tagKeys) {
- OneSignal.getUser().removeTags(RNUtils.convertReableArrayIntoStringCollection(tagKeys));
+ OneSignal.getUser().removeTags(RNUtils.convertReadableArrayIntoStringCollection(tagKeys));
}
@ReactMethod
@@ -601,12 +614,12 @@ public void removeAlias(String label) {
@ReactMethod
public void addAliases(ReadableMap aliases) {
- OneSignal.getUser().addAliases(RNUtils.convertReableMapIntoStringMap(aliases));
+ OneSignal.getUser().addAliases(RNUtils.convertReadableMapIntoStringMap(aliases));
}
@ReactMethod
public void removeAliases(ReadableArray aliasLabels) {
- OneSignal.getUser().removeAliases(RNUtils.convertReableArrayIntoStringCollection(aliasLabels));
+ OneSignal.getUser().removeAliases(RNUtils.convertReadableArrayIntoStringCollection(aliasLabels));
}
/** Added for NativeEventEmitter */
diff --git a/android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java b/android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java
index 50b1b8b4..eeac44fb 100644
--- a/android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java
+++ b/android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java
@@ -12,15 +12,17 @@
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
-// import com.onesignal.OSInAppMessage;
import com.onesignal.inAppMessages.IInAppMessage;
import com.onesignal.inAppMessages.IInAppMessageClickResult;
+import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent;
+import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent;
+import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent;
+import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
import com.onesignal.notifications.INotification;
-import com.onesignal.notifications.INotificationAction;
import com.onesignal.notifications.INotificationClickResult;
import com.onesignal.notifications.INotificationReceivedEvent;
import com.onesignal.user.subscriptions.IPushSubscription;
-import com.onesignal.user.subscriptions.ISubscription;
+import com.onesignal.user.subscriptions.PushSubscriptionState;
import org.json.JSONArray;
import org.json.JSONException;
@@ -109,15 +111,6 @@ public static HashMap convertNotificationToMap(INotification not
return hash;
}
- public static HashMap convertNotificationClickResultToMap(INotificationClickResult openResult) throws JSONException {
- HashMap hash = new HashMap<>();
-
- hash.put("notification", convertNotificationToMap(openResult.getNotification()));
- hash.put("action", convertNotificationActionToMap(openResult.getAction()));
-
- return hash;
- }
-
public static HashMap convertInAppMessageToMap(IInAppMessage message) {
HashMap hash = new HashMap<>();
@@ -126,18 +119,18 @@ public static HashMap convertInAppMessageToMap(IInAppMessage mes
return hash;
}
- public static HashMap convertInAppMessageClickedActionToMap(IInAppMessageClickResult result) {
+ public static HashMap convertInAppMessageClickResultToMap(IInAppMessageClickResult result) {
HashMap hash = new HashMap<>();
- hash.put("clickName", result.getAction().getClickName());
- hash.put("clickUrl", result.getAction().getClickUrl());
- hash.put("firstClick", result.getAction().isFirstClick());
- hash.put("closesMessage", result.getAction().getClosesMessage());
+ hash.put("actionId", result.getActionId());
+ hash.put("urlTarget", result.getUrlTarget());
+ hash.put("url", result.getUrl());
+ hash.put("closingMessage", result.getClosingMessage());
return hash;
}
- public static HashMap convertOnSubscriptionChangedToMap(IPushSubscription state) {
+ public static HashMap convertOnSubscriptionChangedToMap(PushSubscriptionState state) {
HashMap hash = new HashMap<>();
hash.put("token", state.getToken());
@@ -175,7 +168,7 @@ public static HashMap convertJSONObjectToHashMap(JSONObject obje
return hash;
}
- public static Collection convertReableArrayIntoStringCollection(ReadableArray readableArray) {
+ public static Collection convertReadableArrayIntoStringCollection(ReadableArray readableArray) {
ArrayList strings = new ArrayList<>();
for (Object object : readableArray.toArrayList()) {
if (object instanceof String)
@@ -184,7 +177,7 @@ public static Collection convertReableArrayIntoStringCollection(Readable
return strings;
}
- public static HashMap convertReableMapIntoStringMap(ReadableMap readableMap) {
+ public static HashMap convertReadableMapIntoStringMap(ReadableMap readableMap) {
HashMap stringMap = new HashMap<>();
ReadableMapKeySetIterator iter = readableMap.keySetIterator();
@@ -206,22 +199,6 @@ public static HashMap convertPermissionToMap(boolean granted) {
return hash;
}
- private static HashMap convertNotificationActionToMap(INotificationAction action) {
- HashMap hash = new HashMap<>();
-
- hash.put("id", action.getActionId());
-
- switch (action.getType()) {
- case Opened:
- hash.put("type", 0);
- break;
- case ActionTaken:
- hash.put("type", 1);
- }
-
- return hash;
- }
-
private static List