Skip to content

Commit 2082e00

Browse files
inbox: add pushPayload property to IInboxNotification
1 parent f18f34e commit 2082e00

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
INCOMING
2+
----
3+
4+
**Inbox**
5+
- Added `pushPayload` property to `IInboxNotification`.
6+
17
12.0.0
28
----
39

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

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

3+
import android.os.Bundle;
34
import android.util.Log;
45

56
import com.batch.android.BatchInboxNotificationContent;
7+
import com.batch.android.BatchPushPayload;
68
import com.facebook.react.bridge.WritableArray;
79
import com.facebook.react.bridge.WritableMap;
810
import com.facebook.react.bridge.WritableNativeArray;
@@ -11,6 +13,7 @@
1113
import org.json.JSONException;
1214
import org.json.JSONObject;
1315

16+
import java.util.HashMap;
1417
import java.util.List;
1518
import java.util.Map;
1619

@@ -72,6 +75,73 @@ private static WritableMap getWritableMapNotification(BatchInboxNotificationCont
7275

7376
output.putMap("payload", RNUtils.convertMapToWritableMap((Map) notification.getRawPayload()));
7477
output.putBoolean("hasLandingMessage", notification.hasLandingMessage());
78+
output.putMap("pushPayload", buildPushPayload(notification));
79+
7580
return output;
7681
}
82+
83+
private static WritableMap buildPushPayload(BatchInboxNotificationContent notification)
84+
{
85+
WritableMap pushPayloadMap = new WritableNativeMap();
86+
try {
87+
BatchPushPayload pushPayload = notification.getPushPayload();
88+
Map<String, String> rawPayload = (Map<String, String>) notification.getRawPayload();
89+
String batchDataStr = rawPayload.get("com.batch");
90+
91+
boolean isPositiveAction = checkHasScheme(batchDataStr) || pushPayload.getDeeplink() != null;
92+
pushPayloadMap.putBoolean("isPositiveAction", isPositiveAction);
93+
94+
WritableMap batchDataMap = jsonStringToMap(batchDataStr);
95+
pushPayloadMap.putMap("pushPayload", batchDataMap);
96+
97+
String deeplink = pushPayload.getDeeplink();
98+
if (deeplink != null) {
99+
pushPayloadMap.putString("deeplink", deeplink);
100+
} else {
101+
pushPayloadMap.putNull("deeplink");
102+
}
103+
} catch (BatchPushPayload.ParsingException e) {
104+
Log.d(TAG, "Failed to parse push payload: " + e.getMessage());
105+
pushPayloadMap.putBoolean("isPositiveAction", false);
106+
pushPayloadMap.putMap("pushPayload", new WritableNativeMap());
107+
pushPayloadMap.putNull("deeplink");
108+
}
109+
return pushPayloadMap;
110+
}
111+
112+
private static WritableMap jsonStringToMap(String jsonStr)
113+
{
114+
if (jsonStr == null) {
115+
return new WritableNativeMap();
116+
}
117+
118+
try {
119+
JSONObject jsonObj = new JSONObject(jsonStr);
120+
Map<String, Object> map = new HashMap<>();
121+
java.util.Iterator<String> keys = jsonObj.keys();
122+
while (keys.hasNext()) {
123+
String key = keys.next();
124+
map.put(key, jsonObj.get(key));
125+
}
126+
return RNUtils.convertMapToWritableMap(map);
127+
} catch (JSONException e) {
128+
Log.d(TAG, "Failed to parse JSON string: " + e.getMessage());
129+
return new WritableNativeMap();
130+
}
131+
}
132+
133+
private static boolean checkHasScheme(String batchDataStr)
134+
{
135+
if (batchDataStr == null) {
136+
return false;
137+
}
138+
try {
139+
JSONObject batchDataJson = new JSONObject(batchDataStr);
140+
String scheme = batchDataJson.optString("l", "");
141+
return !scheme.isEmpty();
142+
} catch (JSONException e) {
143+
Log.d(TAG, "Could not parse batch data: " + e.getMessage());
144+
return false;
145+
}
146+
}
77147
}

ios/RNBatch.mm

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ - (NSDictionary*) dictionaryWithNotification:(BatchInboxNotificationContent*)not
916916
@"date": [NSNumber numberWithDouble:notification.date.timeIntervalSince1970 * 1000],
917917
@"source": source,
918918
@"payload": notification.payload,
919-
@"hasLandingMessage": @(notification.hasLandingMessage)
919+
@"hasLandingMessage": @(notification.hasLandingMessage),
920+
@"pushPayload": [self dictionaryForPushPayload:notification.payload]
920921
};
921922

922923
NSMutableDictionary *mutableOutput = [output mutableCopy];
@@ -926,8 +927,52 @@ - (NSDictionary*) dictionaryWithNotification:(BatchInboxNotificationContent*)not
926927
if (body != nil) {
927928
mutableOutput[@"body"] = body;
928929
}
929-
output = mutableOutput;
930-
return output;
930+
return [mutableOutput copy];
931+
}
932+
933+
- (NSDictionary*) dictionaryForPushPayload:(NSDictionary*)payload
934+
{
935+
NSMutableDictionary *pushPayloadDict = [NSMutableDictionary new];
936+
937+
NSString *deeplink = nil;
938+
BOOL isPositiveAction = NO;
939+
id batchData = @{};
940+
941+
if ([payload isKindOfClass:[NSDictionary class]]) {
942+
batchData = payload[@"com.batch"] ?: @{};
943+
944+
deeplink = [self extractDeeplinkFromBatchData:batchData];
945+
946+
if (!deeplink) {
947+
id rootDeeplink = payload[@"deeplink"];
948+
if (rootDeeplink && [rootDeeplink isKindOfClass:[NSString class]]) {
949+
deeplink = (NSString *)rootDeeplink;
950+
}
951+
}
952+
953+
isPositiveAction = (deeplink != nil && [deeplink length] > 0);
954+
}
955+
956+
pushPayloadDict[@"isPositiveAction"] = @(isPositiveAction);
957+
pushPayloadDict[@"pushPayload"] = batchData;
958+
pushPayloadDict[@"deeplink"] = deeplink ? deeplink : [NSNull null];
959+
960+
return [pushPayloadDict copy];
961+
}
962+
963+
- (NSString*) extractDeeplinkFromBatchData:(id)batchData
964+
{
965+
if (![batchData isKindOfClass:[NSDictionary class]]) {
966+
return nil;
967+
}
968+
969+
NSDictionary *batchDict = (NSDictionary *)batchData;
970+
id deepLinkValue = batchDict[@"l"];
971+
if (deepLinkValue && [deepLinkValue isKindOfClass:[NSString class]]) {
972+
return (NSString *)deepLinkValue;
973+
}
974+
975+
return nil;
931976
}
932977

933978
// Messaging module

src/BatchInbox.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BatchInboxFetcher } from './BatchInboxFetcher';
2+
import { BatchPushEventPayload } from './BatchPush';
23

34
const RNBatch = require('./NativeRNBatchModule').default;
45

@@ -34,6 +35,11 @@ export interface IInboxNotification {
3435
*/
3536
payload: unknown;
3637

38+
/**
39+
* Push payload associated with this notification
40+
*/
41+
pushPayload: BatchPushEventPayload;
42+
3743
/**
3844
* Date at which the push notification has been sent to the device
3945
*/

0 commit comments

Comments
 (0)