1
1
package com .azure .reactnative .notificationhub ;
2
2
3
+ import android .app .PendingIntent ;
3
4
import android .content .Context ;
4
5
import android .content .Intent ;
5
6
import android .content .SharedPreferences ;
7
+ import android .content .res .Resources ;
8
+ import android .media .RingtoneManager ;
9
+ import android .net .Uri ;
6
10
import android .os .Bundle ;
11
+ import android .util .Log ;
12
+
13
+ import androidx .core .app .NotificationCompat ;
7
14
8
15
import java .util .Arrays ;
9
16
import java .util .HashSet ;
14
21
import com .facebook .react .bridge .ReactContext ;
15
22
import com .microsoft .windowsazure .messaging .NotificationHub ;
16
23
24
+ import org .json .JSONArray ;
17
25
import org .json .JSONException ;
18
26
import org .json .JSONObject ;
19
27
28
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_COVERT_ACTIONS ;
29
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_GET_ACTIONS_ARRAY ;
30
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_INTENT_NOTIFICATION ;
31
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_ACTION ;
32
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_ACTIONS ;
33
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_COLDSTART ;
34
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_FOREGROUND ;
35
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_SMALL_ICON ;
36
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_SOUND_NAME ;
37
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_REMOTE_NOTIFICATION_USER_INTERACTION ;
38
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .REMOTE_NOTIFICATION_PRIORITY_HIGH ;
39
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .REMOTE_NOTIFICATION_PRIORITY_LOW ;
40
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .REMOTE_NOTIFICATION_PRIORITY_MAX ;
41
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .REMOTE_NOTIFICATION_PRIORITY_MIN ;
42
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .REMOTE_NOTIFICATION_PRIORITY_NORMAL ;
43
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .RESOURCE_DEF_TYPE_MIPMAP ;
44
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .RESOURCE_DEF_TYPE_RAW ;
45
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .RESOURCE_NAME_LAUNCHER ;
46
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .RESOURCE_NAME_NOTIFICATION ;
47
+
20
48
public class NotificationHubUtil {
21
49
private static NotificationHubUtil sharedNotificationHubUtilInstance = null ;
22
50
@@ -149,6 +177,10 @@ public boolean getAppIsForeground() {
149
177
return mIsForeground ;
150
178
}
151
179
180
+ public void runInWorkerThread (Runnable runnable ) {
181
+ mPool .execute (runnable );
182
+ }
183
+
152
184
public NotificationHub createNotificationHub (String hubName , String connectionString , ReactContext reactContext ) {
153
185
NotificationHub hub = new NotificationHub (hubName , connectionString , reactContext );
154
186
return hub ;
@@ -175,8 +207,148 @@ public Intent createBroadcastIntent(String action, JSONObject json) {
175
207
return intent ;
176
208
}
177
209
178
- public void runInWorkerThread (Runnable runnable ) {
179
- mPool .execute (runnable );
210
+ public Class getMainActivityClass (Context context ) {
211
+ String packageName = context .getPackageName ();
212
+ Intent launchIntent = context .getPackageManager ().getLaunchIntentForPackage (packageName );
213
+ String className = launchIntent .getComponent ().getClassName ();
214
+ try {
215
+ return Class .forName (className );
216
+ } catch (ClassNotFoundException e ) {
217
+ e .printStackTrace ();
218
+ return null ;
219
+ }
220
+ }
221
+
222
+ public int getNotificationCompatPriority (String priorityString ) {
223
+ int priority = NotificationCompat .PRIORITY_DEFAULT ;
224
+ if (priorityString != null ) {
225
+ switch (priorityString .toLowerCase ()) {
226
+ case REMOTE_NOTIFICATION_PRIORITY_MAX :
227
+ priority = NotificationCompat .PRIORITY_MAX ;
228
+ break ;
229
+ case REMOTE_NOTIFICATION_PRIORITY_HIGH :
230
+ priority = NotificationCompat .PRIORITY_HIGH ;
231
+ break ;
232
+ case REMOTE_NOTIFICATION_PRIORITY_LOW :
233
+ priority = NotificationCompat .PRIORITY_LOW ;
234
+ break ;
235
+ case REMOTE_NOTIFICATION_PRIORITY_MIN :
236
+ priority = NotificationCompat .PRIORITY_MIN ;
237
+ break ;
238
+ case REMOTE_NOTIFICATION_PRIORITY_NORMAL :
239
+ priority = NotificationCompat .PRIORITY_DEFAULT ;
240
+ break ;
241
+ }
242
+ }
243
+
244
+ return priority ;
245
+ }
246
+
247
+ public int getSmallIcon (Bundle bundle , Resources res , String packageName ) {
248
+ int smallIconResId ;
249
+ String smallIcon = bundle .getString (KEY_REMOTE_NOTIFICATION_SMALL_ICON );
250
+
251
+ if (smallIcon != null ) {
252
+ smallIconResId = res .getIdentifier (smallIcon , RESOURCE_DEF_TYPE_MIPMAP , packageName );
253
+ } else {
254
+ smallIconResId = res .getIdentifier (RESOURCE_NAME_NOTIFICATION , RESOURCE_DEF_TYPE_MIPMAP , packageName );
255
+ }
256
+
257
+ if (smallIconResId == 0 ) {
258
+ smallIconResId = res .getIdentifier (RESOURCE_NAME_LAUNCHER , RESOURCE_DEF_TYPE_MIPMAP , packageName );
259
+
260
+ if (smallIconResId == 0 ) {
261
+ smallIconResId = android .R .drawable .ic_dialog_info ;
262
+ }
263
+ }
264
+
265
+ return smallIconResId ;
266
+ }
267
+
268
+ public int getLargeIcon (Bundle bundle , String largeIcon , Resources res , String packageName ) {
269
+ int largeIconResId ;
270
+
271
+ if (largeIcon != null ) {
272
+ largeIconResId = res .getIdentifier (largeIcon , RESOURCE_DEF_TYPE_MIPMAP , packageName );
273
+ } else {
274
+ largeIconResId = res .getIdentifier (RESOURCE_NAME_LAUNCHER , RESOURCE_DEF_TYPE_MIPMAP , packageName );
275
+ }
276
+
277
+ return largeIconResId ;
278
+ }
279
+
280
+ public Uri getSoundUri (Context context , Bundle bundle ) {
281
+ Uri soundUri = RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
282
+ String soundName = bundle .getString (KEY_REMOTE_NOTIFICATION_SOUND_NAME );
283
+ if (soundName != null ) {
284
+ if (!"default" .equalsIgnoreCase (soundName )) {
285
+
286
+ // sound name can be full filename, or just the resource name.
287
+ // So the strings 'my_sound.mp3' AND 'my_sound' are accepted
288
+ // The reason is to make the iOS and android javascript interfaces compatible
289
+
290
+ int resId ;
291
+ if (context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ()) != 0 ) {
292
+ resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
293
+ } else {
294
+ soundName = soundName .substring (0 , soundName .lastIndexOf ('.' ));
295
+ resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
296
+ }
297
+
298
+ soundUri = Uri .parse ("android.resource://" + context .getPackageName () + "/" + resId );
299
+ }
300
+ }
301
+
302
+ return soundUri ;
303
+ }
304
+
305
+ public Intent createNotificationIntent (Context context , Bundle bundle , Class intentClass ) {
306
+ Intent intent = new Intent (context , intentClass );
307
+ intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
308
+ bundle .putBoolean (KEY_REMOTE_NOTIFICATION_FOREGROUND , true );
309
+ bundle .putBoolean (KEY_REMOTE_NOTIFICATION_USER_INTERACTION , false );
310
+ bundle .putBoolean (KEY_REMOTE_NOTIFICATION_COLDSTART , false );
311
+ intent .putExtra (KEY_INTENT_NOTIFICATION , bundle );
312
+
313
+ return intent ;
314
+ }
315
+
316
+ public void processNotificationActions (Context context , Bundle bundle ,
317
+ NotificationCompat .Builder notification ,
318
+ int notificationID ) {
319
+ final String tag = ReactNativeNotificationsHandler .TAG ;
320
+ JSONArray actionsArray = null ;
321
+ try {
322
+ actionsArray = bundle .getString (KEY_REMOTE_NOTIFICATION_ACTIONS ) != null ?
323
+ new JSONArray (bundle .getString (KEY_REMOTE_NOTIFICATION_ACTIONS )) : null ;
324
+ } catch (JSONException e ) {
325
+ Log .e (tag , ERROR_COVERT_ACTIONS , e );
326
+ }
327
+
328
+ if (actionsArray != null ) {
329
+ // No icon for now. The icon value of 0 shows no icon.
330
+ int icon = 0 ;
331
+
332
+ // Add button for each actions.
333
+ for (int i = 0 ; i < actionsArray .length (); i ++) {
334
+ String action ;
335
+ try {
336
+ action = actionsArray .getString (i );
337
+ } catch (JSONException e ) {
338
+ Log .e (tag , ERROR_GET_ACTIONS_ARRAY , e );
339
+ continue ;
340
+ }
341
+
342
+ Intent actionIntent = new Intent ();
343
+ actionIntent .setAction (context .getPackageName () + "." + action );
344
+ // Add "action" for later identifying which button gets pressed.
345
+ bundle .putString (KEY_REMOTE_NOTIFICATION_ACTION , action );
346
+ actionIntent .putExtra (KEY_INTENT_NOTIFICATION , bundle );
347
+ PendingIntent pendingActionIntent = PendingIntent .getBroadcast (context , notificationID , actionIntent ,
348
+ PendingIntent .FLAG_UPDATE_CURRENT );
349
+ notification .addAction (icon , action , pendingActionIntent );
350
+ }
351
+ }
180
352
}
181
353
182
354
private String getPref (Context context , String key ) {
0 commit comments