From 0d9692bb0177d522eed663142ba49cf40c58261a Mon Sep 17 00:00:00 2001 From: vincent-paing Date: Tue, 22 Feb 2022 22:38:36 +0700 Subject: [PATCH] Add setPushToken method This allows the react-native consumer to set the token dynamically which is useful when it has multiple push sdk or consumer refresh token regularly --- README.md | 21 ++++++++++++++++--- .../RNMarketingCloudSdkModule.java | 10 +++++++++ .../RNMarketingCloudSdkModuleTest.java | 12 +++++++++++ ios/RNMarketingCloudSdk.m | 4 ++++ lib/index.js | 11 ++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 44e5a5a..5d71054 100755 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ Follow [these instructions](./ios_push.md) to enable push for iOS. * [.enablePush()](#MCReactModule.enablePush) * [.disablePush()](#MCReactModule.disablePush) * [.getSystemToken()](#MCReactModule.getSystemToken) β‡’ Promise.<?string> + * [.setPushToken(token)](#MCReactModule.setPushToken) * [.getAttributes()](#MCReactModule.getAttributes) β‡’ Promise.<Object.<string, string>> * [.setAttribute(key, value)](#MCReactModule.setAttribute) * [.clearAttribute(key)](#MCReactModule.clearAttribute) @@ -182,6 +183,23 @@ the device. - [Android Docs](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/javadocs/6.0/reference/com/salesforce/marketingcloud/messages/push/PushMessageManager.html#getPushToken()) - [iOS Docs](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/appledoc/Classes/MarketingCloudSDK.html#//api/name/sfmc_deviceToken) + + +### MCReactModule.setPushToken(token) +Set the Firebase or APN token used by the Marketing Cloud to send push messages to +the device. + +**Kind**: static method of [MCReactModule](#MCReactModule) +**See** + +- [Android Docs](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/javadocs/MarketingCloudSdk/7.4/com.salesforce.marketingcloud.messages.push/-push-message-manager/set-push-token.html) +- [iOS Docs](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/appledoc/Classes/MarketingCloudSDK.html#/c:objc(cs)MarketingCloudSDK(im)sfmc_setDeviceToken:) + + +| Param | Type | Description | +| --- | --- | --- | +| token | string | The Firebase or APN token to be used by Marketing Cloud | + ### MCReactModule.getAttributes() β‡’ Promise.<Object.<string, string>> @@ -327,8 +345,5 @@ the SDK and will be requested by the Marketing Cloud support team. **Kind**: static method of [MCReactModule](#MCReactModule) - ---- - ### 3rd Party Product Language Disclaimers Where possible, we changed noninclusive terms to align with our company value of Equality. We retained noninclusive terms to document a third-party system, but we encourage the developer community to embrace more inclusive language. We can update the term when it’s no longer required for technical accuracy. diff --git a/android/src/main/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModule.java b/android/src/main/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModule.java index 2d733a1..99e895b 100644 --- a/android/src/main/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModule.java +++ b/android/src/main/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModule.java @@ -92,6 +92,16 @@ void execute(MarketingCloudSdk sdk, @NonNull Promise promise) { }); } + @ReactMethod + public void setPushToken(final String token) { + handleAction(new Action() { + @Override + void execute(MarketingCloudSdk sdk) { + sdk.getPushMessageManager().setPushToken(token); + } + }); + } + @ReactMethod public void getAttributes(Promise promise) { handleAction(new PromiseAction(promise) { diff --git a/android/src/test/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModuleTest.java b/android/src/test/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModuleTest.java index f48b887..79de736 100755 --- a/android/src/test/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModuleTest.java +++ b/android/src/test/java/com/salesforce/marketingcloud/reactnative/RNMarketingCloudSdkModuleTest.java @@ -151,6 +151,18 @@ public void getSystemToken() { verify(promise).resolve("token"); } + @Test + public void setPushToken() { + // GIVEN + ShadowMarketingCloudSdk.isReady(true); + + // WHEN + reactModule.setPushToken("new-token"); + + // THEN + verify(pushMessageManager).setPushToken("new-token"); + } + @Test public void getAttributes() { // GIVEN diff --git a/ios/RNMarketingCloudSdk.m b/ios/RNMarketingCloudSdk.m index aeb370e..6c5e03e 100644 --- a/ios/RNMarketingCloudSdk.m +++ b/ios/RNMarketingCloudSdk.m @@ -74,6 +74,10 @@ - (void)splitLog:(NSString *)msg { resolve(deviceToken); } +RCT_EXPORT_METHOD(setPushToken : (NSString *_Nonnull)token) { + [[MarketingCloudSDK sharedInstance] sfmc_setDeviceToken:token]; +} + RCT_EXPORT_METHOD(setContactKey : (NSString *_Nonnull)contactKey) { [[MarketingCloudSDK sharedInstance] sfmc_setContactKey:contactKey]; } diff --git a/lib/index.js b/lib/index.js index 7768384..3a6a60d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -48,6 +48,17 @@ class MCReactModule { return RNMarketingCloudSdk.getSystemToken(); } + /** + * Set the Firebase or APN token used by the Marketing Cloud to send push messages to + * the device. + * @param {string} token - The Firebase or APN token to be used by Marketing Cloud + * @see {@link https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/javadocs/MarketingCloudSdk/7.4/com.salesforce.marketingcloud.messages.push/-push-message-manager/set-push-token.html|Android Docs} + * @see {@link https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/appledoc/Classes/MarketingCloudSDK.html#/c:objc(cs)MarketingCloudSDK(im)sfmc_setDeviceToken:|iOS Docs} + */ + static setPushToken(token) { + RNMarketingCloudSdk.setPushToken(token); + } + /** * Returns the maps of attributes set in the registration. * @returns {Promise>} A promise to the key/value map of attributes set