按顺序更新android通知

时间:2015-07-22 12:07:48

标签: android notifications

我正在构建一个消息传递应用程序,在新消息进入时通知用户。

因为这可能每天发生几次(或一小时几次),所以我不想不断地发出新的通知。相反,如果用户没有驳回通知,我想用待处理的新消息的数量更新它(遵循“堆叠”设计指南)。

在Android文档中,有一个使用数字更新通知的示例:

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

但是,这似乎假设您在应用程序内以及通知管理器/构建器之外维护此编号。由于种种原因,这在我的申请中非常不方便(而且很脆弱)。

我想知道 - 有没有办法读取分配给消息的当前号码(相当于mNotifyBuilder.getNumber())?

关注问题:如果无法读取当前号码,是否可以通过正在运行的服务了解用户是否已取消或手动解除通知?

1 个答案:

答案 0 :(得分:0)

您为通知分配了ID。如果用户尚未取消该通知,您可以使用此ID取消或更新相同的通知。

  

直接来自文档:“如果通知具有相同的ID   已经由您的申请发布,但尚未发布   取消后,它将被更新的信息取代。“

请参阅this postthat one