Android: Multiple Notifications as single list in Status Bar

10,437

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

Share:
10,437
Mustansar Saeed
Author by

Mustansar Saeed

I have over 10-years of professional work experience that includes industry and research. My research interests are in systems and span mobile computing and ICT for development. Summary of the experience include: Developed an end-to-end real testbed for on-device machine learning using privacy-preserving machine learning platforms Developed Pakistan's first largest school education system deployed across all schools of Punjab province of Pakistan Developed Pakistan's electronic challan system deployed across Pakistan by NHMP Developed data collecting platform that contributed to raise funding from Gates Foundation Developed eye detection and recognition system for the waste management departments of multiple Punjab districts to mark and record attendance Machine Learning: TensorFlow, PyTorch, pandas, NumPy, Matplotlib, FFmpeg, privacy-preserving machine learning Languages: Android/Kotlin, Java, Python, RxJava2, Room, Retrofit, SQLite Automation: Jenkins Architectures: MVC, MVP, MVVM Source Control: Git, SVN Developer Tools: Google Cloud Platform, Docker, Android Studio, PyCharm, IntelliJ, Eclipse Project Management: Slack, JIRA, TFS, Trello Datastores & web servers: SQLite, MySQL, Apache Tomcat Operating Systems: Android, Ubuntu, Windows I am committed, self-motivated, results-driven and have a good attitude towards problem-solving.

Updated on July 26, 2022

Comments

  • Mustansar Saeed
    Mustansar Saeed almost 2 years

    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++;
        }