Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 419dfa4

Browse files
authored
Merge pull request #1445 from sdenaci/multiple-push-providers
feat: multiple push providers
2 parents 807c6fd + 5716f1e commit 419dfa4

File tree

2 files changed

+215
-187
lines changed

2 files changed

+215
-187
lines changed
Lines changed: 5 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,203 +1,21 @@
11
package com.dieam.reactnativepushnotification.modules;
22

3-
import java.util.Map;
43
import com.google.firebase.messaging.FirebaseMessagingService;
54
import com.google.firebase.messaging.RemoteMessage;
65

7-
import android.app.ActivityManager;
8-
import android.app.ActivityManager.RunningAppProcessInfo;
9-
import android.app.Application;
10-
import android.os.Bundle;
11-
import android.os.Handler;
12-
import android.os.Looper;
13-
import android.util.Log;
14-
15-
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
16-
import com.facebook.react.ReactApplication;
17-
import com.facebook.react.ReactInstanceManager;
18-
import com.facebook.react.bridge.Arguments;
19-
import com.facebook.react.bridge.ReactApplicationContext;
20-
import com.facebook.react.bridge.ReactContext;
21-
import com.facebook.react.bridge.WritableMap;
22-
23-
import org.json.JSONObject;
24-
25-
import java.util.List;
26-
import java.util.Random;
27-
28-
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
6+
import com.dieam.reactnativepushnotification.modules.RNReceivedMessageHandler;
297

308
public class RNPushNotificationListenerService extends FirebaseMessagingService {
319

10+
private RNReceivedMessageHandler mMessageReceivedHandler = new RNReceivedMessageHandler(this);
11+
3212
@Override
3313
public void onNewToken(String token) {
34-
final String deviceToken = token;
35-
Log.d(LOG_TAG, "Refreshed token: " + deviceToken);
36-
37-
Handler handler = new Handler(Looper.getMainLooper());
38-
handler.post(new Runnable() {
39-
public void run() {
40-
// Construct and load our normal React JS code bundle
41-
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
42-
ReactContext context = mReactInstanceManager.getCurrentReactContext();
43-
// If it's constructed, send a notificationre
44-
if (context != null) {
45-
handleNewToken((ReactApplicationContext) context, deviceToken);
46-
} else {
47-
// Otherwise wait for construction, then send the notification
48-
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
49-
public void onReactContextInitialized(ReactContext context) {
50-
handleNewToken((ReactApplicationContext) context, deviceToken);
51-
}
52-
});
53-
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
54-
// Construct it in the background
55-
mReactInstanceManager.createReactContextInBackground();
56-
}
57-
}
58-
}
59-
});
14+
mMessageReceivedHandler.onNewToken(token);
6015
}
6116

62-
private void handleNewToken(ReactApplicationContext context, String token) {
63-
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
64-
65-
WritableMap params = Arguments.createMap();
66-
params.putString("deviceToken", token);
67-
jsDelivery.sendEvent("remoteNotificationsRegistered", params);
68-
}
69-
70-
7117
@Override
7218
public void onMessageReceived(RemoteMessage message) {
73-
String from = message.getFrom();
74-
RemoteMessage.Notification remoteNotification = message.getNotification();
75-
final Bundle bundle = new Bundle();
76-
// Putting it from remoteNotification first so it can be overriden if message
77-
// data has it
78-
if (remoteNotification != null) {
79-
// ^ It's null when message is from GCM
80-
bundle.putString("title", remoteNotification.getTitle());
81-
bundle.putString("message", remoteNotification.getBody());
82-
bundle.putString("sound", remoteNotification.getSound());
83-
bundle.putString("color", remoteNotification.getColor());
84-
}
85-
86-
Map<String, String> notificationData = message.getData();
87-
88-
// Copy `twi_body` to `message` to support Twilio
89-
if (notificationData.containsKey("twi_body")) {
90-
bundle.putString("message", notificationData.get("twi_body"));
91-
}
92-
JSONObject data = getPushData(notificationData.get("data"));
93-
94-
if (data != null) {
95-
if (!bundle.containsKey("message")) {
96-
bundle.putString("message", data.optString("alert", null));
97-
}
98-
if (!bundle.containsKey("title")) {
99-
bundle.putString("title", data.optString("title", null));
100-
}
101-
if (!bundle.containsKey("sound")) {
102-
bundle.putString("soundName", data.optString("sound", null));
103-
}
104-
if (!bundle.containsKey("color")) {
105-
bundle.putString("color", data.optString("color", null));
106-
}
107-
108-
final int badge = data.optInt("badge", -1);
109-
if (badge >= 0) {
110-
ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(this, badge);
111-
}
112-
}
113-
114-
Bundle dataBundle = new Bundle();
115-
for(Map.Entry<String, String> entry : notificationData.entrySet()) {
116-
dataBundle.putString(entry.getKey(), entry.getValue());
117-
}
118-
bundle.putParcelable("data", dataBundle);
119-
120-
Log.v(LOG_TAG, "onMessageReceived: " + bundle);
121-
122-
// We need to run this on the main thread, as the React code assumes that is true.
123-
// Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
124-
// "Can't create handler inside thread that has not called Looper.prepare()"
125-
Handler handler = new Handler(Looper.getMainLooper());
126-
handler.post(new Runnable() {
127-
public void run() {
128-
// Construct and load our normal React JS code bundle
129-
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
130-
ReactContext context = mReactInstanceManager.getCurrentReactContext();
131-
// If it's constructed, send a notificationre
132-
if (context != null) {
133-
handleRemotePushNotification((ReactApplicationContext) context, bundle);
134-
} else {
135-
// Otherwise wait for construction, then send the notification
136-
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
137-
public void onReactContextInitialized(ReactContext context) {
138-
handleRemotePushNotification((ReactApplicationContext) context, bundle);
139-
}
140-
});
141-
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
142-
// Construct it in the background
143-
mReactInstanceManager.createReactContextInBackground();
144-
}
145-
}
146-
}
147-
});
148-
}
149-
150-
private JSONObject getPushData(String dataString) {
151-
try {
152-
return new JSONObject(dataString);
153-
} catch (Exception e) {
154-
return null;
155-
}
156-
}
157-
158-
private void handleRemotePushNotification(ReactApplicationContext context, Bundle bundle) {
159-
160-
// If notification ID is not provided by the user for push notification, generate one at random
161-
if (bundle.getString("id") == null) {
162-
Random randomNumberGenerator = new Random(System.currentTimeMillis());
163-
bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
164-
}
165-
166-
RNPushNotificationConfig config = new RNPushNotificationConfig(getApplication());
167-
168-
Boolean isForeground = isApplicationInForeground();
169-
170-
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
171-
bundle.putBoolean("foreground", isForeground);
172-
bundle.putBoolean("userInteraction", false);
173-
jsDelivery.notifyNotification(bundle);
174-
175-
// If contentAvailable is set to true, then send out a remote fetch event
176-
if (bundle.getString("contentAvailable", "false").equalsIgnoreCase("true")) {
177-
jsDelivery.notifyRemoteFetch(bundle);
178-
}
179-
180-
Log.v(LOG_TAG, "sendNotification: " + bundle);
181-
182-
if (config.getNotificationForeground() || !isForeground) {
183-
Application applicationContext = (Application) context.getApplicationContext();
184-
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
185-
pushNotificationHelper.sendToNotificationCentre(bundle);
186-
}
187-
}
188-
189-
private boolean isApplicationInForeground() {
190-
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
191-
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
192-
if (processInfos != null) {
193-
for (RunningAppProcessInfo processInfo : processInfos) {
194-
if (processInfo.processName.equals(getApplication().getPackageName())
195-
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
196-
&& processInfo.pkgList.length > 0) {
197-
return true;
198-
}
199-
}
200-
}
201-
return false;
19+
mMessageReceivedHandler.handleReceivedMessage(message);
20220
}
20321
}

0 commit comments

Comments
 (0)