I am trying to get local notifications to work like Hangouts. I would like a heads up notification to appear each time a new text message is received. But when there are two or more unread notifications I would like to display a summary notification in the Android notification bar. It seems like stacking notification via groups and adding a group summary should work as described here. The code below seems to work for me on Android 5.0 and 5.1, but on Android 6.0 the local notification does not alarm/display in heads up view when a summary notification for that group exists. So only the initial notification is displayed.
public class MainActivity extends AppCompatActivity {
private Button _button = null;
final static String GROUP_KEY_EMAILS = "group_key_emails";
private int messageNum = 1;
private void CreateNotification() {
// Build the notification, setting the group appropriately
Notification headsUpNotification = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("New Message" + messageNum)
.setSmallIcon(R.drawable.pngreceivedtextmessage)
.setGroup(GROUP_KEY_EMAILS)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(messageNum, headsUpNotification);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.pngreceivedmessageicon);
if(messageNum > 1) {
// Create a summary notification since we have more than 1
Notification summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle("Summary")
.setNumber(messageNum)
.setSmallIcon(R.drawable.pngreceivedtextmessage)
.setLargeIcon(largeIcon)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
notificationManager.notify(0, summaryNotification);
}
messageNum++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_button = (Button) findViewById(R.id.button);
_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CreateNotification();
}
});
}
I am targeting SDK 23 and have tried many different combination but nothing works. Does anyone know how to generate a summary notification and still get heads up notifications to work?
In Android 6.0, there is a slight change in how notifications/summary notifications are displayed. If you keep sending alot of notifications repeatedly, the notification system doesn't show a heads-up display if there is a very short duration between the consecutive notifications.
In order to confirm this, first add this code into the second notification builder inside that if statement (this was missing in your code in order to show it as high priority and with alarm):
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
and then test issuing notification after every couple of seconds (let the previous heads-up notification go away then wait few seconds). If you this correctly you should see the heads-up display with alarm for every notification.
If you start sending notification repeatedly and very fast heads-up wont show up. Just wait 1-2 mins and then issue it again then it shall show up in heads-up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With