Skip to content

Instantly share code, notes, and snippets.

@albeee
Last active April 8, 2019 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save albeee/fd2096e773b4b711a3926504e22b6b9d to your computer and use it in GitHub Desktop.
Save albeee/fd2096e773b4b711a3926504e22b6b9d to your computer and use it in GitHub Desktop.
class NougatNotifications {
/*===============================================================================================*
*========================================== STEP 1 =============================================*
* Bundled notification only works on Android N (API level 24) and above.
* So, you may want to check the platform version everywhere.
*===============================================================================================*/
void step1() {
boolean isBundledNotification = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
if (isBundledNotification) {
//TODO ...
}
}
/*===============================================================================================*
*========================================== STEP 2 =============================================*
* Create a unique notification id for every single notification and summary notification
* id for every summary notification. This is required when you publish a notification in
* the notification tray as well as when you cancel it.
*===============================================================================================*/
void step2() {
//TODO ...
//publish notifications
manger.notifiy(notificationID, notificationObject) |
manger.notifiy(summaryID, summaryNotificationObject)
//cancelling notification
manager.cancel(notificationID);
}
/*===============================================================================================*
*========================================== STEP 3 =============================================*
* Add actions for every individual notification since the user can perform actions on any
* notification if they want.
*===============================================================================================*/
void step3() {
//TODO ...
NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(R.drawable.icon_action_trash,
Globals..getString(R.string.actionbar_option_delete),
deletePendingIntent);
NotificationCompat.Action actionDelete = actionBuilder.build();
builder.addAction(actionDelete);
//TODO ...
}
/*===============================================================================================*
*========================================== STEP 4 =============================================*
* Instead of sending a single notification, send a double notification for every
* message or content. One is called a regular notification with all content and action
* buttons in it and other one is called a summary notification with minimal content.
* Usually you may ask “Why 2 notifications?”.
* Well, this is how they support bundled notification in Nougat.
*===============================================================================================*/
void step4() {
//TODO ...
//Regular notification
regularBuilder.setContentTitle(titleText);
regularBuilder.setContentText(contentText);
regularBuilder.setShowWhen(true);
regularBuilder.setSmallIcon(notificationCount == 1 ? R.drawable.icon_notification_large :
R.drawable.icon_notification_large2ormore);
regularBuilder.setWhen(System.currentTimeMillis());
//Bundled notification
summaryBuilder.setShowWhen(true);
summaryBuilder.setSmallIcon(notificationCount == 1 ? R.drawable.icon_notification_large :
R.drawable.icon_notification_large2ormore);
summaryBuilder.setWhen(System.currentTimeMillis());
summaryBuilder.setSubText(mAccountEmail); //Text appears next to app name - displayable
summaryBuilder.setGroup(mAccountName); //This is the unique key for a single group - non displayable
summaryBuilder.setGroupSummary(true); //This set to be true
//TODO ...
}
/*===============================================================================================*
*========================================== STEP 5 =============================================*
* Sending a bundled notification
*===============================================================================================*/
void step5() {
//TODO ...
if (isBundledNotification) {
//Bundled notifications
Notification reqularNotification = regularBuilder.build();
Notification summaryNotification = summaryBuilder.build();
manager.notify(summaryNotificationID, summaryNotification);
manager.notify(reqularNotificationID, notification);
} else {
//Regular notifications
Notification reqularNotification = regularBuilder.build();
manager.notify(reqularNotificationID, notification);
}
}
/*===============================================================================================*
*========================================== STEP 6 =============================================*
* Cancelling a bundled notification
* Cancel the regular notification and not the summary notification
*===============================================================================================*/
void step6() {
//TODO ...
manager.cancel(reqularNotificationID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment