Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Multiple Notifications as single list in Status Bar

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++;
    }
like image 203
Mustansar Saeed Avatar asked May 30 '14 07:05

Mustansar Saeed


1 Answers

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

like image 112
LordRaydenMK Avatar answered Sep 22 '22 13:09

LordRaydenMK