@@ -127,25 +127,10 @@ private static CharSequence getTitle(JSONObject fcmJson) {
127
127
return currentContext .getPackageManager ().getApplicationLabel (currentContext .getApplicationInfo ());
128
128
}
129
129
130
-
131
- /**
132
- * Notification delete is processed by Broadcast Receiver to avoid creation of activities that can end
133
- * on weird UI interaction
134
- */
135
- private static PendingIntent getNewActionPendingIntent (int requestCode , Intent intent ) {
136
- return PendingIntent .getActivity (currentContext , requestCode , intent , PendingIntent .FLAG_UPDATE_CURRENT );
137
- }
138
-
139
130
private static PendingIntent getNewDismissActionPendingIntent (int requestCode , Intent intent ) {
140
131
return PendingIntent .getBroadcast (currentContext , requestCode , intent , PendingIntent .FLAG_UPDATE_CURRENT );
141
132
}
142
133
143
- private static Intent getNewBaseIntent (int notificationId ) {
144
- return new Intent (currentContext , notificationOpenedClass )
145
- .putExtra (BUNDLE_KEY_ANDROID_NOTIFICATION_ID , notificationId )
146
- .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP | Intent .FLAG_ACTIVITY_CLEAR_TOP );
147
- }
148
-
149
134
private static Intent getNewBaseDismissIntent (int notificationId ) {
150
135
return new Intent (currentContext , notificationDismissedClass )
151
136
.putExtra (BUNDLE_KEY_ANDROID_NOTIFICATION_ID , notificationId )
@@ -274,6 +259,11 @@ private static boolean showNotification(OSNotificationGenerationJob notification
274
259
JSONObject fcmJson = notificationJob .getJsonPayload ();
275
260
String group = fcmJson .optString ("grp" , null );
276
261
262
+ GenerateNotificationOpenIntent intentGenerator = GenerateNotificationOpenIntentFromPushPayload .INSTANCE .create (
263
+ currentContext ,
264
+ fcmJson
265
+ );
266
+
277
267
ArrayList <StatusBarNotification > grouplessNotifs = new ArrayList <>();
278
268
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
279
269
/* Android 7.0 auto groups 4 or more notifications so we find these groupless active
@@ -289,7 +279,13 @@ private static boolean showNotification(OSNotificationGenerationJob notification
289
279
OneSignalNotificationBuilder oneSignalNotificationBuilder = getBaseOneSignalNotificationBuilder (notificationJob );
290
280
NotificationCompat .Builder notifBuilder = oneSignalNotificationBuilder .compatBuilder ;
291
281
292
- addNotificationActionButtons (fcmJson , notifBuilder , notificationId , null );
282
+ addNotificationActionButtons (
283
+ fcmJson ,
284
+ intentGenerator ,
285
+ notifBuilder ,
286
+ notificationId ,
287
+ null
288
+ );
293
289
294
290
try {
295
291
addBackgroundImage (fcmJson , notifBuilder );
@@ -310,17 +306,33 @@ private static boolean showNotification(OSNotificationGenerationJob notification
310
306
311
307
Notification notification ;
312
308
if (group != null ) {
313
- createGenericPendingIntentsForGroup (notifBuilder , fcmJson , group , notificationId );
309
+ createGenericPendingIntentsForGroup (
310
+ notifBuilder ,
311
+ intentGenerator ,
312
+ fcmJson ,
313
+ group ,
314
+ notificationId
315
+ );
314
316
notification = createSingleNotificationBeforeSummaryBuilder (notificationJob , notifBuilder );
315
317
316
318
// Create PendingIntents for notifications in a groupless or defined summary
317
319
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N &&
318
- group .equals (OneSignalNotificationManager .getGrouplessSummaryKey ()))
319
- createGrouplessSummaryNotification (notificationJob , grouplessNotifs .size () + 1 );
320
+ group .equals (OneSignalNotificationManager .getGrouplessSummaryKey ())) {
321
+ createGrouplessSummaryNotification (
322
+ notificationJob ,
323
+ intentGenerator ,
324
+ grouplessNotifs .size () + 1
325
+ );
326
+ }
320
327
else
321
328
createSummaryNotification (notificationJob , oneSignalNotificationBuilder );
322
329
} else {
323
- notification = createGenericPendingIntentsForNotif (notifBuilder , fcmJson , notificationId );
330
+ notification = createGenericPendingIntentsForNotif (
331
+ notifBuilder ,
332
+ intentGenerator ,
333
+ fcmJson ,
334
+ notificationId
335
+ );
324
336
}
325
337
// NotificationManagerCompat does not auto omit the individual notification on the device when using
326
338
// stacked notifications on Android 4.2 and older
@@ -338,18 +350,35 @@ private static boolean showNotification(OSNotificationGenerationJob notification
338
350
return true ;
339
351
}
340
352
341
- private static Notification createGenericPendingIntentsForNotif (NotificationCompat .Builder notifBuilder , JSONObject gcmBundle , int notificationId ) {
353
+ private static Notification createGenericPendingIntentsForNotif (
354
+ NotificationCompat .Builder notifBuilder ,
355
+ GenerateNotificationOpenIntent intentGenerator ,
356
+ JSONObject gcmBundle ,
357
+ int notificationId
358
+ ) {
342
359
Random random = new SecureRandom ();
343
- PendingIntent contentIntent = getNewActionPendingIntent (random .nextInt (), getNewBaseIntent (notificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , gcmBundle .toString ()));
360
+ PendingIntent contentIntent = intentGenerator .getNewActionPendingIntent (
361
+ random .nextInt (),
362
+ intentGenerator .getNewBaseIntent (notificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , gcmBundle .toString ())
363
+ );
344
364
notifBuilder .setContentIntent (contentIntent );
345
365
PendingIntent deleteIntent = getNewDismissActionPendingIntent (random .nextInt (), getNewBaseDismissIntent (notificationId ));
346
366
notifBuilder .setDeleteIntent (deleteIntent );
347
367
return notifBuilder .build ();
348
368
}
349
369
350
- private static void createGenericPendingIntentsForGroup (NotificationCompat .Builder notifBuilder , JSONObject gcmBundle , String group , int notificationId ) {
370
+ private static void createGenericPendingIntentsForGroup (
371
+ NotificationCompat .Builder notifBuilder ,
372
+ GenerateNotificationOpenIntent intentGenerator ,
373
+ JSONObject gcmBundle ,
374
+ String group ,
375
+ int notificationId
376
+ ) {
351
377
Random random = new SecureRandom ();
352
- PendingIntent contentIntent = getNewActionPendingIntent (random .nextInt (), getNewBaseIntent (notificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , gcmBundle .toString ()).putExtra ("grp" , group ));
378
+ PendingIntent contentIntent = intentGenerator .getNewActionPendingIntent (
379
+ random .nextInt (),
380
+ intentGenerator .getNewBaseIntent (notificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , gcmBundle .toString ()).putExtra ("grp" , group )
381
+ );
353
382
notifBuilder .setContentIntent (contentIntent );
354
383
PendingIntent deleteIntent = getNewDismissActionPendingIntent (random .nextInt (), getNewBaseDismissIntent (notificationId ).putExtra ("grp" , group ));
355
384
notifBuilder .setDeleteIntent (deleteIntent );
@@ -450,6 +479,10 @@ static void updateSummaryNotification(OSNotificationGenerationJob notificationJo
450
479
private static void createSummaryNotification (OSNotificationGenerationJob notificationJob , OneSignalNotificationBuilder notifBuilder ) {
451
480
boolean updateSummary = notificationJob .isRestoring ();
452
481
JSONObject fcmJson = notificationJob .getJsonPayload ();
482
+ GenerateNotificationOpenIntent intentGenerator = GenerateNotificationOpenIntentFromPushPayload .INSTANCE .create (
483
+ currentContext ,
484
+ fcmJson
485
+ );
453
486
454
487
String group = fcmJson .optString ("grp" , null );
455
488
@@ -536,7 +569,10 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
536
569
createSummaryIdDatabaseEntry (dbHelper , group , summaryNotificationId );
537
570
}
538
571
539
- PendingIntent summaryContentIntent = getNewActionPendingIntent (random .nextInt (), createBaseSummaryIntent (summaryNotificationId , fcmJson , group ));
572
+ PendingIntent summaryContentIntent = intentGenerator .getNewActionPendingIntent (
573
+ random .nextInt (),
574
+ createBaseSummaryIntent (summaryNotificationId , intentGenerator , fcmJson , group )
575
+ );
540
576
541
577
// 2 or more notifications with a group received, group them together as a single notification.
542
578
if (summaryList != null &&
@@ -622,7 +658,13 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
622
658
// extender setup all the settings will carry over.
623
659
// Note: However their buttons will not carry over as we need to be setup with this new summaryNotificationId.
624
660
summaryBuilder .mActions .clear ();
625
- addNotificationActionButtons (fcmJson , summaryBuilder , summaryNotificationId , group );
661
+ addNotificationActionButtons (
662
+ fcmJson ,
663
+ intentGenerator ,
664
+ summaryBuilder ,
665
+ summaryNotificationId ,
666
+ group
667
+ );
626
668
627
669
summaryBuilder .setContentIntent (summaryContentIntent )
628
670
.setDeleteIntent (summaryDeleteIntent )
@@ -646,7 +688,11 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
646
688
}
647
689
648
690
@ RequiresApi (api = Build .VERSION_CODES .M )
649
- private static void createGrouplessSummaryNotification (OSNotificationGenerationJob notificationJob , int grouplessNotifCount ) {
691
+ private static void createGrouplessSummaryNotification (
692
+ OSNotificationGenerationJob notificationJob ,
693
+ GenerateNotificationOpenIntent intentGenerator ,
694
+ int grouplessNotifCount
695
+ ) {
650
696
JSONObject fcmJson = notificationJob .getJsonPayload ();
651
697
652
698
Notification summaryNotification ;
@@ -656,7 +702,10 @@ private static void createGrouplessSummaryNotification(OSNotificationGenerationJ
656
702
String summaryMessage = grouplessNotifCount + " new messages" ;
657
703
int summaryNotificationId = OneSignalNotificationManager .getGrouplessSummaryId ();
658
704
659
- PendingIntent summaryContentIntent = getNewActionPendingIntent (random .nextInt (), createBaseSummaryIntent (summaryNotificationId , fcmJson , group ));
705
+ PendingIntent summaryContentIntent = intentGenerator .getNewActionPendingIntent (
706
+ random .nextInt (),
707
+ createBaseSummaryIntent (summaryNotificationId ,intentGenerator , fcmJson , group )
708
+ );
660
709
PendingIntent summaryDeleteIntent = getNewDismissActionPendingIntent (random .nextInt (), getNewBaseDismissIntent (0 ).putExtra ("summary" , group ));
661
710
662
711
NotificationCompat .Builder summaryBuilder = getBaseOneSignalNotificationBuilder (notificationJob ).compatBuilder ;
@@ -696,8 +745,13 @@ private static void createGrouplessSummaryNotification(OSNotificationGenerationJ
696
745
NotificationManagerCompat .from (currentContext ).notify (summaryNotificationId , summaryNotification );
697
746
}
698
747
699
- private static Intent createBaseSummaryIntent (int summaryNotificationId , JSONObject fcmJson , String group ) {
700
- return getNewBaseIntent (summaryNotificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , fcmJson .toString ()).putExtra ("summary" , group );
748
+ private static Intent createBaseSummaryIntent (
749
+ int summaryNotificationId ,
750
+ GenerateNotificationOpenIntent intentGenerator ,
751
+ JSONObject fcmJson ,
752
+ String group
753
+ ) {
754
+ return intentGenerator .getNewBaseIntent (summaryNotificationId ).putExtra (BUNDLE_KEY_ONESIGNAL_DATA , fcmJson .toString ()).putExtra ("summary" , group );
701
755
}
702
756
703
757
private static void createSummaryIdDatabaseEntry (OneSignalDbHelper dbHelper , String group , int id ) {
@@ -958,7 +1012,13 @@ static BigInteger getAccentColor(JSONObject fcmJson) {
958
1012
return null ;
959
1013
}
960
1014
961
- private static void addNotificationActionButtons (JSONObject fcmJson , NotificationCompat .Builder mBuilder , int notificationId , String groupSummary ) {
1015
+ private static void addNotificationActionButtons (
1016
+ JSONObject fcmJson ,
1017
+ GenerateNotificationOpenIntent intentGenerator ,
1018
+ NotificationCompat .Builder mBuilder ,
1019
+ int notificationId ,
1020
+ String groupSummary
1021
+ ) {
962
1022
try {
963
1023
JSONObject customJson = new JSONObject (fcmJson .optString ("custom" ));
964
1024
@@ -975,7 +1035,7 @@ private static void addNotificationActionButtons(JSONObject fcmJson, Notificatio
975
1035
JSONObject button = buttons .optJSONObject (i );
976
1036
JSONObject bundle = new JSONObject (fcmJson .toString ());
977
1037
978
- Intent buttonIntent = getNewBaseIntent (notificationId );
1038
+ Intent buttonIntent = intentGenerator . getNewBaseIntent (notificationId );
979
1039
buttonIntent .setAction ("" + i ); // Required to keep each action button from replacing extras of each other
980
1040
buttonIntent .putExtra ("action_button" , true );
981
1041
bundle .put (BUNDLE_KEY_ACTION_ID , button .optString ("id" ));
@@ -985,7 +1045,7 @@ private static void addNotificationActionButtons(JSONObject fcmJson, Notificatio
985
1045
else if (fcmJson .has ("grp" ))
986
1046
buttonIntent .putExtra ("grp" , fcmJson .optString ("grp" ));
987
1047
988
- PendingIntent buttonPIntent = getNewActionPendingIntent (notificationId , buttonIntent );
1048
+ PendingIntent buttonPIntent = intentGenerator . getNewActionPendingIntent (notificationId , buttonIntent );
989
1049
990
1050
int buttonIcon = 0 ;
991
1051
if (button .has ("icon" ))
@@ -1043,4 +1103,4 @@ private static int convertOSToAndroidPriority(int priority) {
1043
1103
1044
1104
return NotificationCompat .PRIORITY_MIN ;
1045
1105
}
1046
- }
1106
+ }
0 commit comments