9

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?

2
  • Did u ask runtime Permissions for Android M? May 9, 2016 at 12:45
  • Did not think I was doing anything that required a permission. Which permission would I need?
    – Alex
    May 9, 2016 at 20:36

2 Answers 2

3
+25

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.

2
  • 1
    Thanks, I changed to code to create a new notification text each time, but the heads up notification still stops working. If I add the priority and defaults to the summary notification, then the summary notification is shown as the heads up notification. That is not what I want, I only want to update the summary but display the regular "New Message" notification as heads up.
    – Alex
    May 9, 2016 at 20:30
  • create the summary first
    – conca
    Jun 19, 2017 at 21:53
1

My wild guess is that since you are reusing the hardcoded 0 as notificationId when invoking notificationManager.notify(0, summaryNotification); - the system could ignore further updates

Try adding an incremental id instead:

In your MainActivity class

  1. add a field private static int lastNotificationId = 0;
  2. use the dynamic id in notificationManager.notify(MainActivity.lastNotificationId++, summaryNotification);

Haven't tried it myself on your code, but I remember I had a similar problem in the past which was solved in this way.

3
  • 1
    Thanks but creating a new id each time for the summary notification did not solve the problem. It ends up creating a new separate notification in the notification bar which forces me to manually cancel the previous one. Also, I am generating a new notification id for the heads up notification already.
    – Alex
    May 11, 2016 at 19:57
  • Then you could avoid manual cancel by user and actually cancel it from code, with notificationManager.cancel(lastNotificationId) before publishing the latest one. Is it placed in the right position you'd want it to be in the notifications bar? Does it trigger the heads-up properly now? (except that the old one is still there..) May 12, 2016 at 9:22
  • No, the heads-up notification is still not triggered correctly.
    – Alex
    May 12, 2016 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.