Android: Multiple Notifications as single list in Status Bar
Asked Answered
P

1

9

I am trying to Notify user based on some criteria. Multiple Notifications are being shown in Status Bar but I want to Group the notification in single notification and when user clicks in the Status Bar, I want to retrieve all notifications in that group. Is that possible? Or I have to maintain PendingIntents of those notifications? Any help will be appreciated. For example, if birthdays of two friends come on same day, then 2 notifications should be shown. I want to combine these notifications i.e. instead of 2 notifications in the status bar, I want one, when user clicks on it, it should have information about 2 notifications. Is it possible?

Please see the code below for displaying notifications.

public void displayNotification(BirthdayDetail detail)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.context);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle(detail.getContactName());
        builder.setContentText(detail.getContactBirthDate());

        Intent resultIntent =  new Intent(this.context, NotificationView.class);
        resultIntent.putExtra("name", detail.getContactName());
        resultIntent.putExtra("birthdate", detail.getContactBDate());
        resultIntent.putExtra("picture_path", detail.getPicturePath());
        resultIntent.putExtra("isContact", detail.isFromContact());
        resultIntent.putExtra("notificationId", notificationId);

        if(detail.isFromContact())
        {
            resultIntent.putExtra("phone_number", detail.getPhoneNumber());
        }

        PendingIntent resultPendingIntent = PendingIntent.getActivity(this.context, requestCode++,
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        notificationManager 
                    = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, builder.build());
        notificationId++;
    }
Pose answered 30/5, 2014 at 7:4 Comment(0)
E
7

When you need to issue a notification multiple times for the same type of event, you should avoid making a completely new notification. Instead, you should consider updating a previous notification, either by changing some of its values or by adding to it, or both.

You can use something like:

mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
    mNotifyBuilder.setContentText(currentText)
        .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

Source: http://developer.android.com/training/notify-user/managing.html

Emrich answered 30/5, 2014 at 7:12 Comment(4)
I want to show multiple at the same time. For example, if birthdays of two friends come on same day, then 2 notifications should be shown. I want to combine these notifications i.e. instead of 2 notifications in the status bar, I want one, when user clicks on it, it should have information about 2 notifications. Is it possible?Pose
The code I gave you is for grouping 2 or more notifications. To handle the click use a PendingIntent corresponding to the number of birthdays.Emrich
SO how do I grab a notification that is already displayed? Then when I get second notification I combine the second with the first. I see you cut and pasted the google code but how do you know how to loop and what do you loop on? This answer does not make that clear.Pacheco
@Pacheco you manage that for yourself depending on your use case. In case of a calendar/birthday app you have the information about the number of birthdays (on a given day) from the start.Emrich

© 2022 - 2024 — McMap. All rights reserved.