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?