Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add messaging custom payload #15

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions android/src/main/java/com/batch/batch_rn/RNBatchInbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,4 @@ private static WritableMap getWritableMapNotification(BatchInboxNotificationCont
output.putBoolean("hasLandingMessage", notification.hasLandingMessage());
return output;
}

private static WritableMap pushPayloadToWritableMap(Map<String, Object> payload)
{
return RNUtils.convertMapToWritableMap(payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
85 changes: 62 additions & 23 deletions android/src/main/java/com/batch/batch_rn/RNUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,8 +34,15 @@ public static WritableMap convertMapToWritableMap(Map<String, Object> input) {
Object value = entry.getValue();
if (value instanceof Map) {
output.putMap(key, convertMapToWritableMap((Map<String, Object>) 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) {
Expand All @@ -44,36 +55,47 @@ public static WritableMap convertMapToWritableMap(Map<String, Object> 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());
}
}
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<String, Object>) 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<String, Object>) 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;
Expand Down Expand Up @@ -144,4 +166,21 @@ public static List<String> 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<String, Object> map = new HashMap<>();
Iterator<String> keys = jsonObject.keys();

while (keys.hasNext()) {
String key = keys.next();
Object value = jsonObject.get(key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ca gere pas les JSONObject recursif (et ptet pas les jsonnull) c'est normal ?

map.put(key, value);
}
return convertMapToWritableMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion ios/RNBatchEventDispatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (void)dequeueEvents {
/// Batch event dispatcher callback
- (void)dispatchEventWithType:(BatchEventDispatcherType)type
payload:(nonnull id<BatchEventDispatcherPayload>)payload {


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

if (payload.sourceMessage != nil) {
BatchMessage* sourceMessage = payload.sourceMessage;
if ([sourceMessage isKindOfClass:BatchInAppMessage.class]) {
output[@"messagingCustomPayload"] = ((BatchInAppMessage*) sourceMessage).customPayload;
}
}

return output;
}

Expand Down
2 changes: 2 additions & 0 deletions src/BatchMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface BatchMessagingEventPayload {
isPositiveAction: boolean;
trackingId?: string | null;
webViewAnalyticsIdentifier?: string | null;
messagingCustomPayload?: Record<string, unknown>; // Custom payload attached to In-App message
pushPayload?: Record<string, unknown>; // Push Payload (only on Mobile Landing event)
deeplink?: string | null;
}

Expand Down
Loading