Skip to content

Commit 30c9334

Browse files
committed
Add android data support
1 parent 8de0165 commit 30c9334

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

android/src/main/java/com/b8ne/RNPusherPushNotifications/PusherWrapper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.pusher.pushnotifications.SubscriptionsChangedListener;
1515
import com.pusher.pushnotifications.PushNotificationReceivedListener;
1616

17+
import java.util.Map.Entry;
18+
1719
//
1820
// TODO: verify the android manifest after https://docs.pusher.com/beams/reference/android
1921
/**
@@ -57,18 +59,24 @@ public void onResume(final Activity activity) {
5759
public void onMessageReceived(RemoteMessage remoteMessage) {
5860
// Arguments.createMap seems to be for testing
5961
// see: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java#L16
60-
//WritableMap map = Arguments.createMap();
62+
// WritableMap map = Arguments.createMap();
6163
final WritableMap map = new WritableNativeMap();
6264
RemoteMessage.Notification notification = remoteMessage.getNotification();
6365

66+
final WritableMap data = new WritableNativeMap();
67+
for (Entry<String, String> entry : remoteMessage.getData().entrySet()) {
68+
data.putString(entry.getKey(), entry.getValue());
69+
}
70+
6471
if(notification != null) {
6572
map.putString("body", notification.getBody());
6673
map.putString("title", notification.getTitle());
6774
map.putString("tag", notification.getTag());
6875
map.putString("click_action", notification.getClickAction());
6976
map.putString("icon", notification.getIcon());
7077
map.putString("color", notification.getColor());
71-
//map.putString("link", notification.getLink());
78+
map.putMap("data", data);
79+
// map.putString("link", notification.getLink());
7280

7381
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(notificationEvent, map);
7482
//System.out.print(remoteMessage.toString());

index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,23 @@ export default {
6161
if (Platform.OS === 'ios') {
6262
rnPusherPushNotificationsEmitter.addListener(eventName, payload => callback(payload));
6363
} else {
64-
DeviceEventEmitter.addListener(eventName, payload => callback(payload));
64+
DeviceEventEmitter.addListener(eventName, payload => {
65+
if (eventName === "notification") {
66+
for (var key in payload.data) {
67+
if (payload.data.hasOwnProperty(key)) {
68+
try {
69+
payload.data[key] = JSON.parse(payload.data[key]);
70+
} catch (err) {
71+
console.log(
72+
"could not convert data in " + key + " to json",
73+
err
74+
);
75+
}
76+
}
77+
}
78+
}
79+
callback(payload);
80+
});
6581
}
66-
},
82+
}
6783
};

0 commit comments

Comments
 (0)