Skip to content

Commit fe4ba12

Browse files
committed
Added the "old" GCM listener to get compatibility with GCM back
1 parent 6b73262 commit fe4ba12

File tree

2 files changed

+150
-5
lines changed

2 files changed

+150
-5
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,26 @@ In your `AndroidManifest.xml`
9898
</intent-filter>
9999
</receiver>
100100
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
101+
102+
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
101103
<service
102-
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
104+
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerServiceGcm"
103105
android:exported="false" >
104106
<intent-filter>
105-
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
106107
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
107-
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
108+
</intent-filter>
109+
</service>
110+
<!-- </ Only if you're using GCM or localNotificationSchedule() > -->
108111

109-
<!-- <Else> -->
112+
<!-- < Else > -->
113+
<service
114+
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
115+
android:exported="false" >
116+
<intent-filter>
110117
<action android:name="com.google.firebase.MESSAGING_EVENT" />
111-
<!-- </Else> -->
112118
</intent-filter>
113119
</service>
120+
<!-- </Else> -->
114121
.....
115122

116123
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.dieam.reactnativepushnotification.modules;
2+
3+
import android.app.ActivityManager;
4+
import android.app.ActivityManager.RunningAppProcessInfo;
5+
import android.app.Application;
6+
import android.os.Bundle;
7+
import android.os.Handler;
8+
import android.os.Looper;
9+
import android.util.Log;
10+
11+
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
12+
import com.facebook.react.ReactApplication;
13+
import com.facebook.react.ReactInstanceManager;
14+
import com.facebook.react.bridge.ReactApplicationContext;
15+
import com.facebook.react.bridge.ReactContext;
16+
import com.google.android.gms.gcm.GcmListenerService;
17+
18+
import org.json.JSONObject;
19+
20+
import java.util.List;
21+
import java.util.Random;
22+
23+
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
24+
25+
public class RNPushNotificationListenerServiceGcm extends GcmListenerService {
26+
27+
@Override
28+
public void onMessageReceived(String from, final Bundle bundle) {
29+
JSONObject data = getPushData(bundle.getString("data"));
30+
// Copy `twi_body` to `message` to support Twilio
31+
if (bundle.containsKey("twi_body")) {
32+
bundle.putString("message", bundle.getString("twi_body"));
33+
}
34+
35+
if (data != null) {
36+
if (!bundle.containsKey("message")) {
37+
bundle.putString("message", data.optString("alert", null));
38+
}
39+
if (!bundle.containsKey("title")) {
40+
bundle.putString("title", data.optString("title", null));
41+
}
42+
if (!bundle.containsKey("sound")) {
43+
bundle.putString("soundName", data.optString("sound", null));
44+
}
45+
if (!bundle.containsKey("color")) {
46+
bundle.putString("color", data.optString("color", null));
47+
}
48+
49+
final int badge = data.optInt("badge", -1);
50+
if (badge >= 0) {
51+
ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(this, badge);
52+
}
53+
}
54+
55+
Log.v(LOG_TAG, "onMessageReceived: " + bundle);
56+
57+
// We need to run this on the main thread, as the React code assumes that is true.
58+
// Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
59+
// "Can't create handler inside thread that has not called Looper.prepare()"
60+
Handler handler = new Handler(Looper.getMainLooper());
61+
handler.post(new Runnable() {
62+
public void run() {
63+
// Construct and load our normal React JS code bundle
64+
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
65+
ReactContext context = mReactInstanceManager.getCurrentReactContext();
66+
// If it's constructed, send a notification
67+
if (context != null) {
68+
handleRemotePushNotification((ReactApplicationContext) context, bundle);
69+
} else {
70+
// Otherwise wait for construction, then send the notification
71+
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
72+
public void onReactContextInitialized(ReactContext context) {
73+
handleRemotePushNotification((ReactApplicationContext) context, bundle);
74+
}
75+
});
76+
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
77+
// Construct it in the background
78+
mReactInstanceManager.createReactContextInBackground();
79+
}
80+
}
81+
}
82+
});
83+
}
84+
85+
private JSONObject getPushData(String dataString) {
86+
try {
87+
return new JSONObject(dataString);
88+
} catch (Exception e) {
89+
return null;
90+
}
91+
}
92+
93+
private void handleRemotePushNotification(ReactApplicationContext context, Bundle bundle) {
94+
95+
// If notification ID is not provided by the user for push notification, generate one at random
96+
if (bundle.getString("id") == null) {
97+
Random randomNumberGenerator = new Random(System.currentTimeMillis());
98+
bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
99+
}
100+
101+
Boolean isForeground = isApplicationInForeground();
102+
103+
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
104+
bundle.putBoolean("foreground", isForeground);
105+
bundle.putBoolean("userInteraction", false);
106+
jsDelivery.notifyNotification(bundle);
107+
108+
// If contentAvailable is set to true, then send out a remote fetch event
109+
if (bundle.getString("contentAvailable", "false").equalsIgnoreCase("true")) {
110+
jsDelivery.notifyRemoteFetch(bundle);
111+
}
112+
113+
Log.v(LOG_TAG, "sendNotification: " + bundle);
114+
115+
if (!isForeground) {
116+
Application applicationContext = (Application) context.getApplicationContext();
117+
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
118+
pushNotificationHelper.sendToNotificationCentre(bundle);
119+
}
120+
}
121+
122+
private boolean isApplicationInForeground() {
123+
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
124+
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
125+
if (processInfos != null) {
126+
for (RunningAppProcessInfo processInfo : processInfos) {
127+
if (processInfo.processName.equals(getApplication().getPackageName())) {
128+
if (processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
129+
for (String d : processInfo.pkgList) {
130+
return true;
131+
}
132+
}
133+
}
134+
}
135+
}
136+
return false;
137+
}
138+
}

0 commit comments

Comments
 (0)