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

时间:2014-05-30 07:04:54

标签: android notifications android-notifications notificationmanager

我正在根据某些条件尝试Notify用户。 Multiple Notifications中显示Status Bar,但我想Group the notification in single notification,当用户点击Status Bar时,我想要检索该群组中的所有通知。那可能吗?或者我必须保留PendingIntents这些通知?任何帮助将不胜感激。例如,如果两个朋友的生日在同一天出现,则应显示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++;
    }

1 个答案:

答案 0 :(得分:7)

  

当您需要为同一类型多次发出通知时   对于事件,您应该避免发出全新的通知。   相反,您应该考虑更新以前的通知   通过改变它的一些值或添加它,或两者兼而有之。

您可以使用以下内容:

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