Skip to content

Commit 0443c85

Browse files
authored
Merge pull request #1578 from OneSignal/fix/grouped_heads_up_showing_last
Fix Heads-Up Displaying Previous Notification When Grouped on some devices
2 parents 0bfcdb2 + 2a11fcf commit 0443c85

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/GenerateNotification.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,26 @@ class GenerateNotification {
8282
private static Resources contextResources = null;
8383
private static Context currentContext = null;
8484
private static String packageName = null;
85+
private static Integer groupAlertBehavior = null;
8586

8687
private static class OneSignalNotificationBuilder {
8788
NotificationCompat.Builder compatBuilder;
8889
boolean hasLargeIcon;
8990
}
9091

92+
// NotificationCompat unfortunately doesn't correctly support some features
93+
// such as sounds and heads-up notifications with GROUP_ALERT_CHILDREN on
94+
// Android 6.0 and older.
95+
// This includes:
96+
// Android 6.0 - No Sound or heads-up
97+
// Android 5.0 - Sound, but no heads-up
98+
private static void initGroupAlertBehavior() {
99+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
100+
groupAlertBehavior = NotificationCompat.GROUP_ALERT_CHILDREN;
101+
else
102+
groupAlertBehavior = NotificationCompat.GROUP_ALERT_SUMMARY;
103+
}
104+
91105
private static void setStatics(Context inContext) {
92106
currentContext = inContext;
93107
packageName = currentContext.getPackageName();
@@ -100,6 +114,8 @@ static boolean displayNotification(OSNotificationGenerationJob notificationJob)
100114

101115
isRunningOnMainThreadCheck();
102116

117+
initGroupAlertBehavior();
118+
103119
return showNotification(notificationJob);
104120
}
105121

@@ -385,7 +401,7 @@ private static void createGenericPendingIntentsForGroup(
385401
notifBuilder.setGroup(group);
386402

387403
try {
388-
notifBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
404+
notifBuilder.setGroupAlertBehavior(groupAlertBehavior);
389405
} catch (Throwable t) {
390406
//do nothing in this case...Android support lib 26 isn't in the project
391407
}
@@ -612,7 +628,7 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
612628
.setGroupSummary(true);
613629

614630
try {
615-
summaryBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
631+
summaryBuilder.setGroupAlertBehavior(groupAlertBehavior);
616632
}
617633
catch (Throwable t) {
618634
//do nothing in this case...Android support lib 26 isn't in the project
@@ -674,7 +690,7 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
674690
.setGroupSummary(true);
675691

676692
try {
677-
summaryBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
693+
summaryBuilder.setGroupAlertBehavior(groupAlertBehavior);
678694
}
679695
catch (Throwable t) {
680696
//do nothing in this case...Android support lib 26 isn't in the project
@@ -730,7 +746,7 @@ private static void createGrouplessSummaryNotification(
730746
.setGroupSummary(true);
731747

732748
try {
733-
summaryBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
749+
summaryBuilder.setGroupAlertBehavior(groupAlertBehavior);
734750
}
735751
catch (Throwable t) {
736752
// Do nothing in this case... Android support lib 26 isn't in the project

0 commit comments

Comments
 (0)