Skip to content

Commit d092f91

Browse files
committed
messaging: added messaging custom payload and push payload to BatchMessagingEventPayload
1 parent 6b015fb commit d092f91

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

android/src/main/java/com/batch/batch_rn/RNBatchEventDispatcher.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package com.batch.batch_rn;
22

33
import android.util.Log;
4+
import android.os.Bundle;
45

56
import androidx.annotation.NonNull;
67
import androidx.annotation.Nullable;
78

89
import com.batch.android.Batch;
910
import com.batch.android.BatchEventDispatcher;
11+
import com.batch.android.BatchMessage;
1012
import com.batch.android.BatchPushPayload;
1113
import com.facebook.react.bridge.Arguments;
1214
import com.facebook.react.bridge.ReactApplicationContext;
1315
import com.facebook.react.bridge.WritableMap;
1416
import com.facebook.react.modules.core.DeviceEventManagerModule;
1517

18+
import org.json.JSONException;
19+
import org.json.JSONObject;
20+
1621
import java.util.LinkedList;
1722

1823
/**
@@ -83,6 +88,30 @@ public void dispatchEvent(@NonNull Batch.EventDispatcher.Type type,
8388
params.putMap("pushPayload", Arguments.fromBundle(pushPayload.getPushBundle()));
8489
}
8590

91+
BatchMessage messagingPayload = payload.getMessagingPayload();
92+
if (messagingPayload != null) {
93+
Bundle bundle = new Bundle();
94+
messagingPayload.writeToBundle(bundle);
95+
Bundle messagingBundle = bundle.getBundle(BatchMessage.MESSAGING_EXTRA_PAYLOAD_KEY);
96+
if (messagingBundle != null) {
97+
Bundle dataBundle = messagingBundle.getBundle("data");
98+
if(dataBundle != null) {
99+
Bundle landingPushPayload = dataBundle.getBundle("batchPushPayload");
100+
if (landingPushPayload != null) {
101+
params.putMap("pushPayload", Arguments.fromBundle(landingPushPayload));
102+
}
103+
String customPayload = dataBundle.getString("custom_payload");
104+
if(customPayload != null) {
105+
try {
106+
JSONObject customPayloadJSON = new JSONObject(customPayload);
107+
params.putMap("messagingCustomPayload", RNUtils.convertJSONObjectToWritableMap(customPayloadJSON));
108+
} catch (JSONException e) {
109+
Log.d(RNBatchModuleImpl.LOGGER_TAG,"Failed to parse messaging custom payload");
110+
}
111+
}
112+
}
113+
}
114+
}
86115
RNBatchEvent event = new RNBatchEvent(eventName, params);
87116
if (!isModuleReady() || !hasListener) {
88117
Log.d(RNBatchModuleImpl.LOGGER_TAG,

android/src/main/java/com/batch/batch_rn/RNUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
import com.facebook.react.bridge.WritableNativeMap;
1515

1616
import org.json.JSONArray;
17+
import org.json.JSONException;
18+
import org.json.JSONObject;
1719

1820
import java.net.URI;
1921
import java.util.ArrayList;
2022
import java.util.Date;
23+
import java.util.HashMap;
24+
import java.util.Iterator;
2125
import java.util.List;
2226
import java.util.Map;
2327

@@ -144,4 +148,22 @@ public static List<String> convertReadableArrayToList(ReadableArray array) {
144148
}
145149
return list;
146150
}
151+
152+
/**
153+
* Convert a JSONObject into a Map
154+
* @param jsonObject The JSONObject to convert
155+
* @return the Map
156+
*/
157+
public static WritableMap convertJSONObjectToWritableMap(JSONObject jsonObject) throws JSONException {
158+
Map<String, Object> map = new HashMap<>();
159+
Iterator<String> keys = jsonObject.keys();
160+
161+
while (keys.hasNext()) {
162+
String key = keys.next();
163+
Object value = jsonObject.get(key);
164+
map.put(key, value);
165+
}
166+
167+
return convertMapToWritableMap(map);
168+
}
147169
}

ios/RNBatchEventDispatcher.mm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ - (void)dequeueEvents {
6767
/// Batch event dispatcher callback
6868
- (void)dispatchEventWithType:(BatchEventDispatcherType)type
6969
payload:(nonnull id<BatchEventDispatcherPayload>)payload {
70-
70+
7171

7272
NSString* eventName = [RNBatchEventDispatcher mapBatchEventDispatcherTypeToRNEvent:type];
7373
if (eventName != nil) {
@@ -124,6 +124,13 @@ - (NSDictionary*) dictionaryWithEventDispatcherPayload:(id<BatchEventDispatcherP
124124
output[@"pushPayload"] = payload.notificationUserInfo;
125125
}
126126

127+
if (payload.sourceMessage != nil) {
128+
BatchMessage* sourceMessage = payload.sourceMessage;
129+
if ([sourceMessage isKindOfClass:BatchInAppMessage.class]) {
130+
output[@"messagingCustomPayload"] = ((BatchInAppMessage*) sourceMessage).customPayload;
131+
}
132+
}
133+
127134
return output;
128135
}
129136

src/BatchMessaging.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface BatchMessagingEventPayload {
66
isPositiveAction: boolean;
77
trackingId?: string | null;
88
webViewAnalyticsIdentifier?: string | null;
9+
messagingCustomPayload?: Record<string, unknown>; // Custom payload attached to In-App message
10+
pushPayload?: Record<string, unknown>; // Push Payload (only on Mobile Landing event)
911
deeplink?: string | null;
1012
}
1113

0 commit comments

Comments
 (0)