android - 在 android 中递增 notification.number

标签 android notifications

我在我的 Android 应用程序中使用通知。每当我收到通知时,我都会尝试使用 notification.number 增加未读通知的数量,如下面的代码所示。

NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number += 1;
Intent intent = new Intent(this, NotificationReceiver.class);

PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notificationManager.notify(0, notification);

但是,无论我收到多少通知,计数器都不会递增。它总是只显示 1。我不明白这里出了什么问题。谁能帮帮我...?

最佳答案

问题是,您正在创建一个新的通知对象。只需创建一次,检查它是否存在于字段中

public class main extends Activity {

Notification notification=null;


//....
if (this.notification == null)
    this.notification = new Notification(R.drawable.icon,
        "A new notification", System.currentTimeMillis());

this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
this.notification.number += 1;

关于android - 在 android 中递增 notification.number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965967/

相关文章:

Javascript未经许可触发声音?

amazon-web-services - 与Amazon Simple Notification Service(sns)相比,使用pubnub的优缺点是什么?

android - Webview 第一次没有加载

android - 将两个布局合并为一个

javascript - 在 Phonegap Android 中存储值的最简单方法

安卓通知删除

ios - 如何检查后台是否有新电子邮件 - ios

android - Android Studio更新后无法启动activity

android - 如何在 android 中使用 GridView 创建自定义警报对话框?

macos - 当日期改变时 NSNotification?