@@ -80,7 +80,7 @@ private static class OneSignalNotificationBuilder {
80
80
boolean hasLargeIcon ;
81
81
}
82
82
83
- static void setStatics (Context inContext ) {
83
+ private static void setStatics (Context inContext ) {
84
84
currentContext = inContext ;
85
85
packageName = currentContext .getPackageName ();
86
86
contextResources = currentContext .getResources ();
@@ -313,8 +313,18 @@ private static void showNotification(NotificationGenerationJob notifJob) {
313
313
OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Could not set background notification image!" , t );
314
314
}
315
315
316
- if (notifJob .overrideSettings != null && notifJob .overrideSettings .extender != null )
316
+ if (notifJob .overrideSettings != null && notifJob .overrideSettings .extender != null ) {
317
+ notifJob .orgFlags = notifBuilder .mNotification .flags ;
318
+ notifJob .orgSound = notifBuilder .mNotification .sound ;
317
319
notifBuilder .extend (notifJob .overrideSettings .extender );
320
+
321
+ notifJob .overriddenBodyFromExtender = notifBuilder .mContentText ;
322
+ notifJob .overriddenTitleFromExtender = notifBuilder .mContentTitle ;
323
+ if (!notifJob .restoring ) {
324
+ notifJob .overriddenFlags = notifBuilder .mNotification .flags ;
325
+ notifJob .overriddenSound = notifBuilder .mNotification .sound ;
326
+ }
327
+ }
318
328
319
329
// Keeps notification from playing sound + vibrating again
320
330
if (notifJob .restoring )
@@ -329,7 +339,8 @@ private static void showNotification(NotificationGenerationJob notifJob) {
329
339
notifBuilder .setDeleteIntent (deleteIntent );
330
340
notifBuilder .setGroup (group );
331
341
332
- notification = notifBuilder .build ();
342
+ notification = createSingleNotificationBeforeSummaryBuilder (notifJob , notifBuilder );
343
+
333
344
createSummaryNotification (notifJob , oneSignalNotificationBuilder );
334
345
}
335
346
else {
@@ -351,6 +362,28 @@ private static void showNotification(NotificationGenerationJob notifJob) {
351
362
NotificationManagerCompat .from (currentContext ).notify (notificationId , notification );
352
363
}
353
364
}
365
+
366
+ // Removes custom sound set from the extender from non-summary notification before building it.
367
+ // This prevents the sound from playing twice or both the default sound + a custom one.
368
+ private static Notification createSingleNotificationBeforeSummaryBuilder (NotificationGenerationJob notifJob , NotificationCompat .Builder notifBuilder ) {
369
+ // Includes Android 4.3 through 6.0.1. Android 7.1 handles this correctly without this.
370
+ // Android 4.2 and older just post the summary only.
371
+ boolean singleNotifWorkArounds = Build .VERSION .SDK_INT > Build .VERSION_CODES .JELLY_BEAN_MR1 && Build .VERSION .SDK_INT <Build .VERSION_CODES .N && !notifJob .restoring ;
372
+
373
+ if (singleNotifWorkArounds ) {
374
+ if (notifJob .overriddenSound != null && !notifJob .overriddenSound .equals (notifJob .orgSound ))
375
+ notifBuilder .setSound (null );
376
+ }
377
+
378
+ Notification notification = notifBuilder .build ();
379
+
380
+
381
+ if (singleNotifWorkArounds ) {
382
+ notifBuilder .setSound (notifJob .overriddenSound );
383
+ }
384
+
385
+ return notification ;
386
+ }
354
387
355
388
// Xiaomi requires the following to show a custom notification icons.
356
389
// Without this MIUI 8 will only show the app icon on the left.
@@ -382,7 +415,7 @@ static void updateSummaryNotification(NotificationGenerationJob notifJob) {
382
415
}
383
416
384
417
// This summary notification will be visible instead of the normal one on pre-Android 7.0 devices.
385
- static void createSummaryNotification (NotificationGenerationJob notifJob , OneSignalNotificationBuilder notifBuilder ) {
418
+ private static void createSummaryNotification (NotificationGenerationJob notifJob , OneSignalNotificationBuilder notifBuilder ) {
386
419
boolean updateSummary = notifJob .restoring ;
387
420
JSONObject gcmBundle = notifJob .jsonPayload ;
388
421
@@ -493,6 +526,13 @@ static void createSummaryNotification(NotificationGenerationJob notifJob, OneSig
493
526
NotificationCompat .Builder summaryBuilder = getBaseOneSignalNotificationBuilder (notifJob ).compatBuilder ;
494
527
if (updateSummary )
495
528
removeNotifyOptions (summaryBuilder );
529
+ else {
530
+ if (notifJob .overriddenSound != null )
531
+ summaryBuilder .setSound (notifJob .overriddenSound );
532
+
533
+ if (notifJob .overriddenFlags != null )
534
+ summaryBuilder .setDefaults (notifJob .overriddenFlags );
535
+ }
496
536
497
537
// The summary is designed to fit all notifications.
498
538
// Default small and large icons are used instead of the payload options to enforce this.
@@ -514,14 +554,18 @@ static void createSummaryNotification(NotificationGenerationJob notifJob, OneSig
514
554
515
555
// Add the latest notification to the summary
516
556
if (!updateSummary ) {
517
- String line1Title = gcmBundle .optString ("title" , null );
557
+ String line1Title = null ;
558
+
559
+ if (notifJob .getTitle () != null )
560
+ line1Title = notifJob .getTitle ().toString ();
518
561
519
562
if (line1Title == null )
520
563
line1Title = "" ;
521
564
else
522
565
line1Title += " " ;
523
-
524
- String message = gcmBundle .optString ("alert" );
566
+
567
+ String message = notifJob .getBody ().toString ();
568
+
525
569
SpannableString spannableString = new SpannableString (line1Title + message );
526
570
if (line1Title .length () > 0 )
527
571
spannableString .setSpan (new StyleSpan (android .graphics .Typeface .BOLD ), 0 , line1Title .length (), 0 );
0 commit comments