Skip to content

Commit b567750

Browse files
authored
Merge pull request #475 from Iterable/jay/MOB-5621-custom-payload
[MOB-5621] align RN side data type for in-app customPayload field
2 parents aa92c7f + 77a3db1 commit b567750

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

android/src/main/java/com/iterable/iterableapi/RNIterableInternal.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
import androidx.annotation.Nullable;
44

5+
import com.facebook.react.bridge.WritableArray;
6+
import com.facebook.react.bridge.WritableMap;
7+
import com.facebook.react.bridge.WritableNativeArray;
8+
import com.facebook.react.bridge.WritableNativeMap;
9+
10+
import org.json.JSONArray;
11+
import org.json.JSONException;
512
import org.json.JSONObject;
613

14+
import java.util.Iterator;
15+
716
public class RNIterableInternal {
817

918
private static String TAG = "RNIterableInternal";
@@ -17,7 +26,17 @@ public static String getUserId() {
1726
}
1827

1928
public static JSONObject getInAppMessageJson(IterableInAppMessage message) {
20-
return message.toJSONObject();
29+
JSONObject messageJson = message.toJSONObject();
30+
31+
if (message.getCustomPayload() != null) {
32+
try {
33+
messageJson.put(IterableConstants.ITERABLE_IN_APP_CUSTOM_PAYLOAD, convertJsonToMap(message.getCustomPayload()));
34+
} catch (JSONException e) {
35+
// something crazy happened if we're here so we'll just skip
36+
}
37+
}
38+
39+
return messageJson;
2140
}
2241

2342
public static IterableInAppMessage getMessageById(String messageId) {
@@ -32,4 +51,56 @@ public static void setAttributionInfo(IterableAttributionInfo attributionInfo) {
3251
IterableApi.getInstance().setAttributionInfo(attributionInfo);
3352
}
3453

54+
// obtained from https://gist.github.com/viperwarp/2beb6bbefcc268dee7ad
55+
// copied over from Serialization.java for expediency, this can definitely be reorganized better
56+
57+
static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
58+
WritableMap map = new WritableNativeMap();
59+
60+
Iterator<String> iterator = jsonObject.keys();
61+
while (iterator.hasNext()) {
62+
String key = iterator.next();
63+
Object value = jsonObject.get(key);
64+
if (value instanceof JSONObject) {
65+
map.putMap(key, convertJsonToMap((JSONObject) value));
66+
} else if (value instanceof JSONArray) {
67+
map.putArray(key, convertJsonToArray((JSONArray) value));
68+
} else if (value instanceof Boolean) {
69+
map.putBoolean(key, (Boolean) value);
70+
} else if (value instanceof Integer) {
71+
map.putInt(key, (Integer) value);
72+
} else if (value instanceof Double) {
73+
map.putDouble(key, (Double) value);
74+
} else if (value instanceof String) {
75+
map.putString(key, (String) value);
76+
} else {
77+
map.putString(key, value.toString());
78+
}
79+
}
80+
return map;
81+
}
82+
83+
static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
84+
WritableArray array = new WritableNativeArray();
85+
86+
for (int i = 0; i < jsonArray.length(); i++) {
87+
Object value = jsonArray.get(i);
88+
if (value instanceof JSONObject) {
89+
array.pushMap(convertJsonToMap((JSONObject) value));
90+
} else if (value instanceof JSONArray) {
91+
array.pushArray(convertJsonToArray((JSONArray) value));
92+
} else if (value instanceof Boolean) {
93+
array.pushBoolean((Boolean) value);
94+
} else if (value instanceof Integer) {
95+
array.pushInt((Integer) value);
96+
} else if (value instanceof Double) {
97+
array.pushDouble((Double) value);
98+
} else if (value instanceof String) {
99+
array.pushString((String) value);
100+
} else {
101+
array.pushString(value.toString());
102+
}
103+
}
104+
return array;
105+
}
35106
}

android/src/main/java/com/iterable/reactnative/RNIterableAPIModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void initializeWithApiKey(String apiKey, ReadableMap configReadableMap, S
109109
@ReactMethod
110110
public void setEmail(@Nullable String email) {
111111
IterableLogger.d(TAG, "setEmail: " + email);
112+
112113
IterableApi.getInstance().setEmail(email);
113114
}
114115

android/src/main/java/com/iterable/reactnative/Serialization.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static List<IterableInboxSession.Impression> impressionsFromReadableArray(Readab
263263
// ---------------------------------------------------------------------------------------
264264
// region React Native JSON conversion methods
265265
// obtained from https://gist.github.com/viperwarp/2beb6bbefcc268dee7ad
266-
266+
267267
static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
268268
WritableMap map = new WritableNativeMap();
269269

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class ReactIterableAPI: RCTEventEmitter {
345345
func getInAppMessages(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
346346
ITBInfo()
347347

348-
resolver(IterableAPI.inAppManager.getMessages().map{ $0.toDict() })
348+
resolver(IterableAPI.inAppManager.getMessages().map { $0.toDict() })
349349
}
350350

351351
@objc(getInboxMessages:rejecter:)

ios/RNIterableAPI/Serialization.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extension IterableInAppMessage {
159159
dict["customPayload"] = customPayload
160160
dict["read"] = read
161161
dict["priorityLevel"] = priorityLevel
162+
162163
return dict
163164
}
164165
}

ts/IterableInAppMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class IterableInAppMessage {
5252
/**
5353
* Custom Payload for this message.
5454
*/
55-
readonly customPayload?: any
55+
readonly customPayload?: {any: any}
5656

5757
/**
5858
* Whether this inbox message has been read
@@ -71,7 +71,7 @@ class IterableInAppMessage {
7171
expiresAt: Date | undefined,
7272
saveToInbox: boolean,
7373
inboxMetadata: IterableInboxMetadata | undefined,
74-
customPayload: any | undefined,
74+
customPayload: {any: any} | undefined,
7575
read: boolean,
7676
priorityLevel: number) {
7777
this.campaignId = campaignId

ts/__tests__/IterableInApp.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ beforeEach(() => {
2828

2929
test('trackInAppOpen_params_methodCalledWithParams', () => {
3030
// GIVEN an in-app message and a location
31-
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { CustomPayloadKey: 'CustomPayloadValue' }, false, 300.5)
31+
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { 'CustomPayloadKey': 'CustomPayloadValue' }, false, 300.5)
3232
const location: IterableInAppLocation = IterableInAppLocation.inApp
3333

3434
// WHEN Iterable.trackInAppOpen is called
@@ -40,7 +40,7 @@ test('trackInAppOpen_params_methodCalledWithParams', () => {
4040

4141
test('trackInAppClick_params_methodCalledWithParams', () => {
4242
// GIVEN an in-app message, a location, and a url
43-
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { CustomPayloadKey: 'CustomPayloadValue' }, false, 300.5)
43+
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { 'CustomPayloadKey': 'CustomPayloadValue' }, false, 300.5)
4444
const location: IterableInAppLocation = IterableInAppLocation.inApp
4545
const url: string = 'URLClicked'
4646

@@ -53,7 +53,7 @@ test('trackInAppClick_params_methodCalledWithParams', () => {
5353

5454
test('trackInAppClose_params_methodCalledWithParams', () => {
5555
// GIVEN an in-app messsage, a location, a close source, and a url
56-
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { CustomPayloadKey: 'CustomPayloadValue' }, false, 300.5)
56+
const msg: IterableInAppMessage = new IterableInAppMessage('someMessageId', 123, new IterableInAppTrigger(IterableInAppTriggerType.event), new Date(1234), new Date(123123), true, new IterableInboxMetadata('title', 'subtitle', 'iconURL'), { 'CustomPayloadKey': 'CustomPayloadValue' }, false, 300.5)
5757
const location: IterableInAppLocation = IterableInAppLocation.inbox
5858
const source: IterableInAppCloseSource = IterableInAppCloseSource.link
5959
const url: string = 'ClickedURL'

0 commit comments

Comments
 (0)