Skip to content

Commit d72be09

Browse files
authored
Merge pull request #55 from vborisov/master
Allow adding of the extra data parameter to the notification response
2 parents 3396fb9 + 8c7f337 commit d72be09

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.b8ne.RNPusherPushNotifications;
2+
3+
import android.app.Activity;
4+
5+
import android.os.Handler;
6+
import android.os.Bundle;
7+
import android.os.Looper;
8+
import android.content.Intent;
9+
10+
import java.util.Map;
11+
12+
import com.facebook.react.ReactApplication;
13+
import com.facebook.react.ReactInstanceManager;
14+
import com.facebook.react.bridge.Arguments;
15+
import com.facebook.react.bridge.ReactContext;
16+
import com.facebook.react.bridge.WritableMap;
17+
import com.facebook.react.bridge.WritableNativeMap;
18+
import com.facebook.react.modules.core.DeviceEventManagerModule;
19+
20+
public class NotificationsMessagingService {
21+
22+
private static String notificationEvent = "notification";
23+
private static ReactContext context;
24+
private static ReactInstanceManager reactInstanceManager;
25+
26+
public static void read(final ReactInstanceManager reactInstanceManager, Activity reactActivity) {
27+
Intent intent = reactActivity.getIntent();
28+
final WritableMap map = new WritableNativeMap();
29+
30+
boolean launchedFromHistory = intent != null ? (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0 : false;
31+
32+
Bundle extras = intent.getExtras();
33+
if (!launchedFromHistory & extras != null) {
34+
WritableMap payload = Arguments.createMap();
35+
36+
for (String key : extras.keySet()) {
37+
Object value = extras.get(key);
38+
payload.putString(key, value.toString());
39+
}
40+
map.putMap("data", payload);
41+
42+
if (payload != null) {
43+
// We need to run this on the main thread, as the React code assumes that is true.
44+
// Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
45+
// "Can't create handler inside thread that has not called Looper.prepare()"
46+
Handler handler = new Handler(Looper.getMainLooper());
47+
handler.post(new Runnable() {
48+
public void run() {
49+
// Construct and load our normal React JS code data
50+
// ReactInstanceManager reactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
51+
ReactContext context = reactInstanceManager.getCurrentReactContext();
52+
// If it's constructed, send a notification
53+
if (context != null) {
54+
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
55+
.emit(notificationEvent, map);
56+
} else {
57+
// Otherwise wait for construction, then send the notification
58+
reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
59+
public void onReactContextInitialized(ReactContext context) {
60+
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
61+
.emit(notificationEvent, map);
62+
}
63+
});
64+
if (!reactInstanceManager.hasStartedCreatingInitialContext()) {
65+
// Construct it in the background
66+
reactInstanceManager.createReactContextInBackground();
67+
}
68+
}
69+
}
70+
});
71+
}
72+
}
73+
}
74+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.app.Activity;
66

77
import java.util.Set;
8+
import java.util.Map;
89

910
import com.facebook.react.bridge.*;
1011
import com.facebook.react.modules.core.DeviceEventManagerModule;
@@ -63,6 +64,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
6364
// WritableMap map = Arguments.createMap();
6465
final WritableMap map = new WritableNativeMap();
6566
RemoteMessage.Notification notification = remoteMessage.getNotification();
67+
Map<String, String> data = remoteMessage.getData();
6668

6769
if (notification != null) {
6870
map.putString("body", notification.getBody());
@@ -73,6 +75,16 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
7375
map.putString("color", notification.getColor());
7476
// map.putString("link", notification.getLink());
7577

78+
if (data != null) {
79+
WritableMap payload = Arguments.createMap();
80+
81+
for(Map.Entry<String,String> entry : data.entrySet()) {
82+
payload.putString(entry.getKey(), entry.getValue());
83+
}
84+
85+
map.putMap("data", payload);
86+
}
87+
7688
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
7789
.emit(notificationEvent, map);
7890
// System.out.print(remoteMessage.toString());

0 commit comments

Comments
 (0)