This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRNPushNotification.java
More file actions
356 lines (298 loc) · 11.6 KB
/
RNPushNotification.java
File metadata and controls
356 lines (298 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.dieam.reactnativepushnotification.modules;
import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener {
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag
public static final String KEY_TEXT_REPLY = "key_text_reply";
public interface RNIntentHandler {
void onNewIntent(Intent intent);
@Nullable
Bundle getBundleFromIntent(Intent intent);
}
public static ArrayList<RNIntentHandler> IntentHandlers = new ArrayList();
private RNPushNotificationHelper mRNPushNotificationHelper;
private final SecureRandom mRandomNumberGenerator = new SecureRandom();
private RNPushNotificationJsDelivery mJsDelivery;
public RNPushNotification(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
Application applicationContext = (Application) reactContext.getApplicationContext();
// The @ReactNative methods use this
mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext);
// This is used to delivery callbacks to JS
mJsDelivery = new RNPushNotificationJsDelivery(reactContext);
}
@Override
public String getName() {
return "ReactNativePushNotification";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
return constants;
}
private Bundle getBundleFromIntent(Intent intent) {
Bundle bundle = null;
if (intent.hasExtra("notification")) {
bundle = intent.getBundleExtra("notification");
} else if (intent.hasExtra("google.message_id")) {
bundle = new Bundle();
bundle.putBundle("data", intent.getExtras());
}
if (bundle == null) {
for (RNIntentHandler handler : IntentHandlers) {
bundle = handler.getBundleFromIntent(intent);
}
}
if(null != bundle && !bundle.getBoolean("foreground", false) && !bundle.containsKey("userInteraction")) {
bundle.putBoolean("userInteraction", true);
}
return bundle;
}
@Override
public void onNewIntent(Intent intent) {
for (RNIntentHandler handler : IntentHandlers) {
handler.onNewIntent(intent);
}
Bundle bundle = this.getBundleFromIntent(intent);
if (bundle != null) {
mJsDelivery.notifyNotification(bundle);
}
}
@ReactMethod
public void invokeApp(ReadableMap data) {
Bundle bundle = null;
if (data != null) {
bundle = Arguments.toBundle(data);
}
mRNPushNotificationHelper.invokeApp(bundle);
}
@ReactMethod
public void checkPermissions(Promise promise) {
ReactContext reactContext = getReactApplicationContext();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(reactContext);
promise.resolve(managerCompat.areNotificationsEnabled());
}
@ReactMethod
public void requestPermissions() {
final RNPushNotificationJsDelivery fMjsDelivery = mJsDelivery;
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.e(LOG_TAG, "exception", task.getException());
return;
}
WritableMap params = Arguments.createMap();
params.putString("deviceToken", task.getResult());
fMjsDelivery.sendEvent("remoteNotificationsRegistered", params);
}
});
}
@ReactMethod
public void subscribeToTopic(String topic) {
FirebaseMessaging.getInstance().subscribeToTopic(topic);
}
@ReactMethod
public void unsubscribeFromTopic(String topic) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
}
@ReactMethod
public void getFCMToken(final Promise promise) {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.e(LOG_TAG, "exception", task.getException());
promise.reject(task.getException());
} else {
promise.resolve(task.getResult());
}
});
}
@ReactMethod
public void presentLocalNotification(ReadableMap details) {
Bundle bundle = Arguments.toBundle(details);
// If notification ID is not provided by the user, generate one at random
if (bundle.getString("id") == null) {
bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
}
mRNPushNotificationHelper.sendToNotificationCentre(bundle);
}
@ReactMethod
public void scheduleLocalNotification(ReadableMap details) {
Bundle bundle = Arguments.toBundle(details);
// If notification ID is not provided by the user, generate one at random
if (bundle.getString("id") == null) {
bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
}
mRNPushNotificationHelper.sendNotificationScheduled(bundle);
}
@ReactMethod
public void getInitialNotification(Promise promise) {
WritableMap params = Arguments.createMap();
Activity activity = getCurrentActivity();
if (activity != null) {
Bundle bundle = this.getBundleFromIntent(activity.getIntent());
if (bundle != null) {
bundle.putBoolean("foreground", false);
String bundleString = mJsDelivery.convertJSON(bundle);
params.putString("dataJSON", bundleString);
}
}
promise.resolve(params);
}
@ReactMethod
public void setApplicationIconBadgeNumber(int number) {
ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(getReactApplicationContext(), number);
}
// removed @Override temporarily just to get it working on different versions of RN
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
onActivityResult(requestCode, resultCode, data);
}
// removed @Override temporarily just to get it working on different versions of RN
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Ignored, required to implement ActivityEventListener for RN 0.33
}
@ReactMethod
/**
* Cancels all scheduled local notifications, and removes all entries from the notification
* centre.
*
*/
public void cancelAllLocalNotifications() {
mRNPushNotificationHelper.cancelAllScheduledNotifications();
mRNPushNotificationHelper.clearNotifications();
}
@ReactMethod
/**
* Cancel scheduled notification, and remove notification from the notification centre.
*
*/
public void cancelLocalNotification(String notification_id) {
mRNPushNotificationHelper.cancelScheduledNotification(notification_id);
}
@ReactMethod
/**
* Clear notification from the notification centre.
*/
public void clearLocalNotification(String tag, int notificationID) {
mRNPushNotificationHelper.clearNotification(tag, notificationID);
}
@ReactMethod
/**
* Clears all notifications from the notification center
*
*/
public void removeAllDeliveredNotifications() {
mRNPushNotificationHelper.clearNotifications();
}
@ReactMethod
/**
* Returns a list of all notifications currently in the Notification Center
*/
public void getDeliveredNotifications(Callback callback) {
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
}
@ReactMethod
/**
* Returns a list of all currently scheduled notifications
*/
public void getScheduledLocalNotifications(Callback callback) {
callback.invoke(mRNPushNotificationHelper.getScheduledLocalNotifications());
}
@ReactMethod
/**
* Removes notifications from the Notification Center, whose id matches
* an element in the provided array
*/
public void removeDeliveredNotifications(ReadableArray identifiers) {
mRNPushNotificationHelper.clearDeliveredNotifications(identifiers);
}
@ReactMethod
/**
* Unregister for all remote notifications received
*/
public void abandonPermissions() {
FirebaseMessaging.getInstance().deleteToken();
Log.i(LOG_TAG, "InstanceID deleted");
}
@ReactMethod
/**
* List all channels id
*/
public void getChannels(Callback callback) {
WritableArray array = Arguments.fromList(mRNPushNotificationHelper.listChannels());
if(callback != null) {
callback.invoke(array);
}
}
@ReactMethod
/**
* Check if channel exists with a given id
*/
public void channelExists(String channel_id, Callback callback) {
boolean exists = mRNPushNotificationHelper.channelExists(channel_id);
if(callback != null) {
callback.invoke(exists);
}
}
@ReactMethod
/**
* Creates a channel if it does not already exist. Returns whether the channel was created.
*/
public void createChannel(ReadableMap channelInfo, Callback callback) {
boolean created = mRNPushNotificationHelper.createChannel(channelInfo);
if(callback != null) {
callback.invoke(created);
}
}
@ReactMethod
/**
* Check if channel is blocked with a given id
*/
public void channelBlocked(String channel_id, Callback callback) {
boolean blocked = mRNPushNotificationHelper.channelBlocked(channel_id);
if(callback != null) {
callback.invoke(blocked);
}
}
@ReactMethod
/**
* Delete channel with a given id
*/
public void deleteChannel(String channel_id) {
mRNPushNotificationHelper.deleteChannel(channel_id);
}
}