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 2 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
22 changes: 22 additions & 0 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 Down Expand Up @@ -144,4 +148,22 @@ 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);
}
}
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