|
1 | 1 | package com.dieam.reactnativepushnotification.modules;
|
2 | 2 |
|
3 |
| -import java.util.Map; |
4 | 3 | import com.google.firebase.messaging.FirebaseMessagingService;
|
5 | 4 | import com.google.firebase.messaging.RemoteMessage;
|
6 | 5 |
|
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; |
29 | 7 |
|
30 | 8 | public class RNPushNotificationListenerService extends FirebaseMessagingService {
|
31 | 9 |
|
| 10 | + private RNReceivedMessageHandler mMessageReceivedHandler = new RNReceivedMessageHandler(this); |
| 11 | + |
32 | 12 | @Override
|
33 | 13 | 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); |
60 | 15 | }
|
61 | 16 |
|
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 |
| - |
71 | 17 | @Override
|
72 | 18 | 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); |
202 | 20 | }
|
203 | 21 | }
|
0 commit comments