Skip to content

Commit 2a11fcf

Browse files
committed
Fix group compatibility Android 6 and lower
After testing Android 6 in the Android Studio emulator it was found that using setGroupAlertBehavior with GROUP_ALERT_CHILDREN breaks the heads-up and sound for notifications that are grouped. Since the GROUP_ALERT_CHILDREN flag is required for newer versions of Android to fix a different bug with heads up notifications from showing old notifications in the group we needed to add an Android version runtime check to select the correct flag. We could report this bug to Google and they might be able to fix it in a future AndroidX release however due to the age of Android 6 and it's limitations it is unlikely it will be fixed. So this workaround will mostly likely need to stay in place until we drop support for Android 6. See the last commit for more details on why GROUP_ALERT_CHILDREN is needed for Android 8+. This also addresses the failing test form the last commit.
1 parent 51ae1ee commit 2a11fcf

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_CHILDREN);
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_CHILDREN);
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_CHILDREN);
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_CHILDREN);
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)