From d092f914a20ec7e26dfc25d2dca8c691594c6ed0 Mon Sep 17 00:00:00 2001 From: Arnaud Roland Date: Thu, 28 Nov 2024 16:30:21 +0100 Subject: [PATCH 1/4] messaging: added messaging custom payload and push payload to BatchMessagingEventPayload --- .../batch_rn/RNBatchEventDispatcher.java | 29 +++++++++++++++++++ .../main/java/com/batch/batch_rn/RNUtils.java | 22 ++++++++++++++ ios/RNBatchEventDispatcher.mm | 9 +++++- src/BatchMessaging.ts | 2 ++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/batch/batch_rn/RNBatchEventDispatcher.java b/android/src/main/java/com/batch/batch_rn/RNBatchEventDispatcher.java index 9c437cc..ca7c78a 100644 --- a/android/src/main/java/com/batch/batch_rn/RNBatchEventDispatcher.java +++ b/android/src/main/java/com/batch/batch_rn/RNBatchEventDispatcher.java @@ -1,18 +1,23 @@ package com.batch.batch_rn; import android.util.Log; +import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.batch.android.Batch; import com.batch.android.BatchEventDispatcher; +import com.batch.android.BatchMessage; import com.batch.android.BatchPushPayload; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.LinkedList; /** @@ -83,6 +88,30 @@ public void dispatchEvent(@NonNull Batch.EventDispatcher.Type type, params.putMap("pushPayload", Arguments.fromBundle(pushPayload.getPushBundle())); } + BatchMessage messagingPayload = payload.getMessagingPayload(); + if (messagingPayload != null) { + Bundle bundle = new Bundle(); + messagingPayload.writeToBundle(bundle); + Bundle messagingBundle = bundle.getBundle(BatchMessage.MESSAGING_EXTRA_PAYLOAD_KEY); + if (messagingBundle != null) { + Bundle dataBundle = messagingBundle.getBundle("data"); + if(dataBundle != null) { + Bundle landingPushPayload = dataBundle.getBundle("batchPushPayload"); + if (landingPushPayload != null) { + params.putMap("pushPayload", Arguments.fromBundle(landingPushPayload)); + } + String customPayload = dataBundle.getString("custom_payload"); + if(customPayload != null) { + try { + JSONObject customPayloadJSON = new JSONObject(customPayload); + params.putMap("messagingCustomPayload", RNUtils.convertJSONObjectToWritableMap(customPayloadJSON)); + } catch (JSONException e) { + Log.d(RNBatchModuleImpl.LOGGER_TAG,"Failed to parse messaging custom payload"); + } + } + } + } + } RNBatchEvent event = new RNBatchEvent(eventName, params); if (!isModuleReady() || !hasListener) { Log.d(RNBatchModuleImpl.LOGGER_TAG, diff --git a/android/src/main/java/com/batch/batch_rn/RNUtils.java b/android/src/main/java/com/batch/batch_rn/RNUtils.java index 9681d3c..ef55c5b 100644 --- a/android/src/main/java/com/batch/batch_rn/RNUtils.java +++ b/android/src/main/java/com/batch/batch_rn/RNUtils.java @@ -14,10 +14,14 @@ import com.facebook.react.bridge.WritableNativeMap; import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import java.net.URI; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -144,4 +148,22 @@ public static List convertReadableArrayToList(ReadableArray array) { } return list; } + + /** + * Convert a JSONObject into a Map + * @param jsonObject The JSONObject to convert + * @return the Map + */ + public static WritableMap convertJSONObjectToWritableMap(JSONObject jsonObject) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = jsonObject.keys(); + + while (keys.hasNext()) { + String key = keys.next(); + Object value = jsonObject.get(key); + map.put(key, value); + } + + return convertMapToWritableMap(map); + } } diff --git a/ios/RNBatchEventDispatcher.mm b/ios/RNBatchEventDispatcher.mm index cf639b5..ada841f 100644 --- a/ios/RNBatchEventDispatcher.mm +++ b/ios/RNBatchEventDispatcher.mm @@ -67,7 +67,7 @@ - (void)dequeueEvents { /// Batch event dispatcher callback - (void)dispatchEventWithType:(BatchEventDispatcherType)type payload:(nonnull id)payload { - + NSString* eventName = [RNBatchEventDispatcher mapBatchEventDispatcherTypeToRNEvent:type]; if (eventName != nil) { @@ -124,6 +124,13 @@ - (NSDictionary*) dictionaryWithEventDispatcherPayload:(id; // Custom payload attached to In-App message + pushPayload?: Record; // Push Payload (only on Mobile Landing event) deeplink?: string | null; } From 16de60f46d0ce8c65eff34302d2c3c410ac3ddc7 Mon Sep 17 00:00:00 2001 From: Arnaud Roland Date: Thu, 28 Nov 2024 16:35:10 +0100 Subject: [PATCH 2/4] all: update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bbde09..6989301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ UPCOMING - Added `setPhoneNumber` API to the `BatchProfileAttributeEditor` class. This requires to have a user identifier registered or to call the `identify` method beforehand. - Added `setSMSMarketingSubscription` API to the `BatchProfileAttributeEditor` class. +**Messaging** +- Added `messagingCustomPayload` property to `BatchMessagingEventPayload` (only for In-App Message). +- Added `pushPayload` property to `BatchMessagingEventPayload` (only for Landing Mobile). + 9.0.2 ---- From b1ea6e04fecb2feb76affae8849dc26cb2111c4e Mon Sep 17 00:00:00 2001 From: Arnaud Roland Date: Tue, 3 Dec 2024 14:41:36 +0100 Subject: [PATCH 3/4] android: fix parsing json object and json null values --- .../java/com/batch/batch_rn/RNBatchInbox.java | 5 -- .../main/java/com/batch/batch_rn/RNUtils.java | 65 ++++++++++++------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/android/src/main/java/com/batch/batch_rn/RNBatchInbox.java b/android/src/main/java/com/batch/batch_rn/RNBatchInbox.java index 7521c4c..76eac0f 100644 --- a/android/src/main/java/com/batch/batch_rn/RNBatchInbox.java +++ b/android/src/main/java/com/batch/batch_rn/RNBatchInbox.java @@ -73,9 +73,4 @@ private static WritableMap getWritableMapNotification(BatchInboxNotificationCont output.putBoolean("hasLandingMessage", notification.hasLandingMessage()); return output; } - - private static WritableMap pushPayloadToWritableMap(Map payload) - { - return RNUtils.convertMapToWritableMap(payload); - } } diff --git a/android/src/main/java/com/batch/batch_rn/RNUtils.java b/android/src/main/java/com/batch/batch_rn/RNUtils.java index ef55c5b..d3e7637 100644 --- a/android/src/main/java/com/batch/batch_rn/RNUtils.java +++ b/android/src/main/java/com/batch/batch_rn/RNUtils.java @@ -34,8 +34,15 @@ public static WritableMap convertMapToWritableMap(Map input) { Object value = entry.getValue(); if (value instanceof Map) { output.putMap(key, convertMapToWritableMap((Map) value)); - } else if (value instanceof JSONArray) { - output.putArray(key, convertArrayToWritableArray((Object[]) value)); + } else if (value instanceof JSONObject) { + try { + output.putMap(key, convertJSONObjectToWritableMap((JSONObject) value)); + } catch (JSONException e) { + Log.e(RNBatchModuleImpl.LOGGER_TAG, "Failed to parse JSON Object.", e); + } + } + else if (value instanceof JSONArray) { + output.putArray(key, convertArrayToWritableArray((JSONArray) value)); } else if (value instanceof Boolean) { output.putBoolean(key, (Boolean) value); } else if (value instanceof Integer) { @@ -48,6 +55,8 @@ public static WritableMap convertMapToWritableMap(Map input) { output.putDouble(key, ((Date) value).getTime()); } else if (value instanceof URI) { output.putString(key, value.toString()); + } else if (value == null || value == JSONObject.NULL) { + output.putNull(key); } else { output.putString(key, value.toString()); } @@ -55,29 +64,38 @@ public static WritableMap convertMapToWritableMap(Map input) { return output; } - public static WritableArray convertArrayToWritableArray(Object[] input) { + public static WritableArray convertArrayToWritableArray(JSONArray input) { WritableArray output = new WritableNativeArray(); - for (int i = 0; i < input.length; i++) { - Object value = input[i]; - if (value instanceof Map) { - output.pushMap(convertMapToWritableMap((Map) value)); - } else if (value instanceof JSONArray) { - output.pushArray(convertArrayToWritableArray((Object[]) value)); - } else if (value instanceof Boolean) { - output.pushBoolean((Boolean) value); - } else if (value instanceof Integer) { - output.pushInt((Integer) value); - } else if (value instanceof Double) { - output.pushDouble((Double) value); - } else if (value instanceof String) { - output.pushString((String) value); - } else if (value instanceof Date) { - output.pushDouble(((Date) value).getTime()); - } else if (value instanceof URI) { - output.pushString(value.toString()); - } else { - output.pushString(value.toString()); + for (int i = 0; i < input.length(); i++) { + Object value = null; + try { + value = input.get(i); + if (value instanceof Map) { + output.pushMap(convertMapToWritableMap((Map) value)); + } + else if (value instanceof JSONObject) { + output.pushMap(convertJSONObjectToWritableMap((JSONObject) value)); + } + else if (value instanceof JSONArray) { + output.pushArray(convertArrayToWritableArray((JSONArray) value)); + } else if (value instanceof Boolean) { + output.pushBoolean((Boolean) value); + } else if (value instanceof Integer) { + output.pushInt((Integer) value); + } else if (value instanceof Double) { + output.pushDouble((Double) value); + } else if (value instanceof String) { + output.pushString((String) value); + } else if (value instanceof Date) { + output.pushDouble(((Date) value).getTime()); + } else if (value instanceof URI) { + output.pushString(value.toString()); + } else { + output.pushString(value.toString()); + } + } catch (JSONException e) { + Log.e(RNBatchModuleImpl.LOGGER_TAG, "Failed to parse JSON Array.", e); } } return output; @@ -163,7 +181,6 @@ public static WritableMap convertJSONObjectToWritableMap(JSONObject jsonObject) Object value = jsonObject.get(key); map.put(key, value); } - return convertMapToWritableMap(map); } } From c56ddeaa253d3d2b28e8edf5d448330ba5c34583 Mon Sep 17 00:00:00 2001 From: Arnaud Roland Date: Tue, 3 Dec 2024 15:09:08 +0100 Subject: [PATCH 4/4] inbox: fix android displayLandingMessage --- .../src/main/java/com/batch/batch_rn/RNBatchModuleImpl.java | 4 ++-- .../src/newarch/java/com/batch/batch_rn/RNBatchModule.java | 2 +- .../src/oldarch/java/com/batch/batch_rn/RNBatchModule.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/batch/batch_rn/RNBatchModuleImpl.java b/android/src/main/java/com/batch/batch_rn/RNBatchModuleImpl.java index dc7b870..165c2b0 100644 --- a/android/src/main/java/com/batch/batch_rn/RNBatchModuleImpl.java +++ b/android/src/main/java/com/batch/batch_rn/RNBatchModuleImpl.java @@ -442,7 +442,7 @@ public void onFetchFailure(@NonNull String error) { }); } - public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String notificationIdentifier, final Promise promise) { + public void inbox_fetcher_displayLandingMessage(Activity currentActivity, String fetcherIdentifier, String notificationIdentifier, final Promise promise) { if (!this.batchInboxFetcherMap.containsKey(fetcherIdentifier)) { promise.reject("InboxError", "FETCHER_NOT_FOUND"); return; @@ -455,7 +455,7 @@ public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String promise.reject("InboxError", "NOTIFICATION_NOT_FOUND"); return; } - notification.displayLandingMessage(reactContext); + notification.displayLandingMessage(currentActivity); promise.resolve(null); } diff --git a/android/src/newarch/java/com/batch/batch_rn/RNBatchModule.java b/android/src/newarch/java/com/batch/batch_rn/RNBatchModule.java index d649ea2..e8fde3a 100644 --- a/android/src/newarch/java/com/batch/batch_rn/RNBatchModule.java +++ b/android/src/newarch/java/com/batch/batch_rn/RNBatchModule.java @@ -194,7 +194,7 @@ public void inbox_fetcher_fetchNextPage(String fetcherIdentifier, Promise promis @Override public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String notificationIdentifier, Promise promise) { - impl.inbox_fetcher_displayLandingMessage(fetcherIdentifier, notificationIdentifier, promise); + impl.inbox_fetcher_displayLandingMessage(getCurrentActivity(), fetcherIdentifier, notificationIdentifier, promise); } // USER MODULE diff --git a/android/src/oldarch/java/com/batch/batch_rn/RNBatchModule.java b/android/src/oldarch/java/com/batch/batch_rn/RNBatchModule.java index 1da828d..b12b7ba 100644 --- a/android/src/oldarch/java/com/batch/batch_rn/RNBatchModule.java +++ b/android/src/oldarch/java/com/batch/batch_rn/RNBatchModule.java @@ -200,7 +200,7 @@ public void inbox_fetcher_fetchNextPage(String fetcherIdentifier, final Promise @ReactMethod public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String notificationIdentifier, final Promise promise) { - impl.inbox_fetcher_displayLandingMessage(fetcherIdentifier, notificationIdentifier, promise); + impl.inbox_fetcher_displayLandingMessage(getCurrentActivity(), fetcherIdentifier, notificationIdentifier, promise); } // USER MODULE