From ac0dfef5067a83fd789eff4a007900b57ae8a1ff Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:28:37 -0800 Subject: [PATCH 1/5] bump native sdk version dependencies to 5.0.5 --- android/build.gradle | 2 +- react-native-onesignal.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 805c3e7e..04918c25 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.4' + api 'com.onesignal:OneSignal:5.0.5' testImplementation 'junit:junit:4.12' } \ No newline at end of file diff --git a/react-native-onesignal.podspec b/react-native-onesignal.podspec index a7447852..20392019 100644 --- a/react-native-onesignal.podspec +++ b/react-native-onesignal.podspec @@ -22,5 +22,5 @@ Pod::Spec.new do |s| # pod 'React', :path => '../node_modules/react-native/' # The Native OneSignal-iOS-SDK XCFramework from cocoapods. - s.dependency 'OneSignalXCFramework', '5.0.4' + s.dependency 'OneSignalXCFramework', '5.0.5' end From bcaa0d4d64fbc84096ae2cf4e7cab4c40ece42d1 Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:40:16 -0800 Subject: [PATCH 2/5] add getTags method --- .../rnonesignalandroid/RNOneSignal.java | 18 +++++++++++++++++- ios/RCTOneSignal/RCTOneSignalEventEmitter.m | 5 +++++ src/index.ts | 9 +++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java index b17e614d..7a922f8a 100644 --- a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java +++ b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java @@ -49,6 +49,8 @@ of this software and associated documentation files (the "Software"), to deal import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.Arguments; import com.onesignal.Continue; import com.onesignal.OneSignal; import com.onesignal.debug.LogLevel; @@ -73,9 +75,9 @@ of this software and associated documentation files (the "Software"), to deal import com.onesignal.user.subscriptions.PushSubscriptionState; import com.onesignal.user.subscriptions.PushSubscriptionChangedState; import org.json.JSONException; -import org.json.JSONObject; import java.util.HashMap; +import java.util.Map; public class RNOneSignal extends ReactContextBaseJavaModule implements IPushSubscriptionObserver, @@ -573,6 +575,20 @@ public void removeTags(ReadableArray tagKeys) { OneSignal.getUser().removeTags(RNUtils.convertReadableArrayIntoStringCollection(tagKeys)); } + @ReactMethod + public void getTags(Promise promise) { + try { + Map tags = OneSignal.getUser().getTags(); + WritableMap writableTags = Arguments.createMap(); + for (Map.Entry entry : tags.entrySet()) { + writableTags.putString(entry.getKey(), entry.getValue()); + } + promise.resolve(writableTags); + } catch (Throwable t) { + promise.reject(t.getMessage()); + } + } + @ReactMethod public void addEmail(String email, Promise promise) { try { diff --git a/ios/RCTOneSignal/RCTOneSignalEventEmitter.m b/ios/RCTOneSignal/RCTOneSignalEventEmitter.m index 1e459ae1..1c72d7dd 100644 --- a/ios/RCTOneSignal/RCTOneSignalEventEmitter.m +++ b/ios/RCTOneSignal/RCTOneSignalEventEmitter.m @@ -357,6 +357,11 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body { [OneSignal.User removeTags:keys]; } +RCT_EXPORT_METHOD(getTags:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { + NSDictionary *tags = [OneSignal.User getTags]; + resolve(tags); +} + RCT_EXPORT_METHOD(addAlias:(NSString *)label :(NSString *)id) { [OneSignal.User addAliasWithLabel:label id:id]; } diff --git a/src/index.ts b/src/index.ts index ad0223ab..ed6d2e67 100644 --- a/src/index.ts +++ b/src/index.ts @@ -391,6 +391,15 @@ export namespace OneSignal { RNOneSignal.removeTags(keys); } + + /** Returns the local tags for the current user. */ + export function getTags(): Promise<{ [key: string]: string }> { + if (!isNativeModuleLoaded(RNOneSignal)) { + return Promise.reject(new Error('OneSignal native module not loaded')); + } + + return RNOneSignal.getTags(); + } } export namespace Notifications { From f759eca435f1ac4849390de997017f9e9d19267d Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:41:24 -0800 Subject: [PATCH 3/5] add getTags button to example app --- examples/RNOneSignalTS/src/OSButtons.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/RNOneSignalTS/src/OSButtons.tsx b/examples/RNOneSignalTS/src/OSButtons.tsx index 20e73a65..d9a4914f 100644 --- a/examples/RNOneSignalTS/src/OSButtons.tsx +++ b/examples/RNOneSignalTS/src/OSButtons.tsx @@ -276,6 +276,11 @@ class OSButtons extends React.Component { OneSignal.User.removeTags(['my_tag1', 'my_tag2']); }); + const getTagsButton = renderButtonView('Get tags', async () => { + const tags = await OneSignal.User.getTags(); + loggingFunction('Tags:', tags); + }); + const setLanguageButton = renderButtonView('Set Language', () => { loggingFunction( 'Attempting to set language: ', @@ -346,6 +351,7 @@ class OSButtons extends React.Component { deleteTagWithKeyButton, addTagsButton, removeTagsButton, + getTagsButton, setLanguageButton, addSmsButton, removeSmsButton, From c076c2806294f246495988fd3d99fc5ebdffce1e Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:49:39 -0800 Subject: [PATCH 4/5] Update migration guide to include getTags --- MIGRATION_GUIDE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index 36dae091..54d15b49 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -211,7 +211,7 @@ The User namespace is accessible via `OneSignal.User` and provides access to use | `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._| ## Push Subscription Namespace The Push Subscription namespace is accessible via `OneSignal.User.pushSubscription` and provides access to push subscription-scoped functionality. From f5d1e2775b7b7f9a508d9c9efab429f5c7ec5be1 Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Mon, 4 Dec 2023 20:10:49 -0800 Subject: [PATCH 5/5] remove try/catch block from getTags The try/catch block is unnecessary since creating a new JSONObject from a Map will not throw a JSONException. --- .../onesignal/rnonesignalandroid/RNOneSignal.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java index 7a922f8a..36713cc4 100644 --- a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java +++ b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java @@ -577,16 +577,12 @@ public void removeTags(ReadableArray tagKeys) { @ReactMethod public void getTags(Promise promise) { - try { - Map tags = OneSignal.getUser().getTags(); - WritableMap writableTags = Arguments.createMap(); - for (Map.Entry entry : tags.entrySet()) { - writableTags.putString(entry.getKey(), entry.getValue()); - } - promise.resolve(writableTags); - } catch (Throwable t) { - promise.reject(t.getMessage()); + Map tags = OneSignal.getUser().getTags(); + WritableMap writableTags = Arguments.createMap(); + for (Map.Entry entry : tags.entrySet()) { + writableTags.putString(entry.getKey(), entry.getValue()); } + promise.resolve(writableTags); } @ReactMethod