Android group notification does not cause alarm after adding a summary notification (Android 6.0)

I am trying to get local notifications to work like Hangouts. I would like a notification of votes to appear every time I receive a new text message. But when there are two or more unread notifications, I would like to display a summary notification in the Android notification bar. It seems that stacking notifications through groups and adding a group summary should work as described here . The code seems to work for me on Android 5.0 and 5.1, but on Android 6.0 the local notification does not display the alarm / display as headers when there is a summary notification for this group. Thus, 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 combinations, but nothing works. Does anyone know how to generate a summary notification and still receive header notifications for work?

+7
android android-6.0-marshmallow android-notifications
source share
2 answers

In Android 6.0, there is a slight change in the way notifications / summary notifications are displayed. If you constantly send many notifications, the notification system does not display heads-up if there is a very short duration between successive notifications.

To confirm this, first add this code to the second notification constructor inside this if statement (this was not enough in the code to show it as a high priority and with a warning):

 .setPriority(Notification.PRIORITY_HIGH) .setDefaults(Notification.DEFAULT_ALL) 

and then test the release notice every couple of seconds (let the previous heads-up notification disappear, and then wait a few seconds). If you are correct, you should see an alarm display for each notification.

If you start sending notifications several times, and very fast heads-ups will not appear. Just wait 1-2 minutes, and then release it again, then it will appear in heads-up.

+3
source share

My wild assumption is that since you are reusing hardcoded 0 as a notification, when calling notificationManager.notify(0, summaryNotification); - the system may ignore further updates

Try adding incremental id:

In your MainActivity class

  • add field private static int lastNotificationId = 0;
  • use the dynamic identifier in notificationManager.notify(MainActivity.lastNotificationId++, summaryNotification);

I have not tried it myself according to your code, but I remember that I had a similar problem in the past, which was solved in this way.

+1
source share

All Articles