Android:状态栏中的多个通知作为单个列表

标签 android notifications android-notifications notificationmanager

我正在尝试根据某些标准通知 用户。 Multiple Notifications 显示在 Status Bar 但我想 Group the notifications in single notification 并且当用户点击 Status Bar ,我想检索该组中的所有通知。那可能吗?或者我必须维护这些通知的 PendingIntents?任何帮助将不胜感激。例如,如果两个 friend 的生日在同一天,则应显示 2 个通知。我想合并这些通知,即我想要一个,而不是状态栏中的 2 个通知,当用户单击它时,它应该包含有关 2 个通知的信息。可能吗?

请看下面显示通知的代码。

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

最佳答案

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.

你可以使用类似的东西:

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());

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

关于Android:状态栏中的多个通知作为单个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23948924/

相关文章:

c# - 具有多列的Android Listview

android - 如何将字符串从 Retrofit 转换为我的数据类模型中的自定义对象?

Swift 3 - 未触发本地通知的 didReceiveRemoteNotification 函数

c# - Windows Server 2008 中没有气球工具提示关闭按钮

android - 如何在特定时间在android上发出通知?

在折叠模式下带有图像的 Android 通知

java - 下载 PDF 不起作用

c# - 当实体匹配用户定义的查询/过滤器/规则时执行操作

android - 如何在 Android 中启用或禁用通知 LED?

android - 我无法使用 Intent 进入下一个 Activity