java - Android:管理多个通知

标签 java android

我正在尝试在我的应用程序中创建多个通知。为了唯一地标识每个通知,我给了他们一个唯一的 identificationId。以下是我的代码:

private void updateNotification(int notificationId, int clockStatusID, CharSequence text) {
 //notificationManager.cancel(notificationId);
// throws up an ongoing notification that the timer is running
Log.i("TIMERCOUNT", "Notification id: " + notificationId);
Notification not = new Notification(clockStatusID, // the
    // icon
    // for
    // the
    // status
    // bar
    text, // the text to display in the ticker
    System.currentTimeMillis() // the timestamp for the
    // notification to appear
);
Intent intent = new Intent();
intent.putExtra("notificationID", notificationId);
intent.setAction("actionstring" + System.currentTimeMillis());
intent.setClassName("com.chander.time.android.activities",
"com.chander.time.android.activities.Tabs");


not.setLatestEventInfo(self,
    getText(R.string.timer_notification_title),
    getText(R.string.timer_on_notification_text), PendingIntent
    .getActivity(this, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT));

not.flags += Notification.FLAG_ONGOING_EVENT;
not.flags += Notification.FLAG_NO_CLEAR;
notificationManager.notify(notificationId, not);
}

问题: 选择通知时,将调用 Tabs Activity 传递 Intent 。我想访问在选项卡中选择的通知的唯一 notificationId。我尝试了 intent.putExtra() 将 notificationId 保存在 Intent 中。但是,对于多个通知,它会覆盖 notificationId 并返回最新的通知。我不明白为什么会这样,我怎样才能避免覆盖 notificationId。

谢谢, 钱德

最佳答案

我得到了答案。 Intent 被缓存。要创建一个新的 Intent ,只需添加以下代码:

saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));

这使得 Intent 独一无二。

此外,有人建议我使用以下代码来使 Intent 独一无二:

saveCallIntent.setAction("actionstring" + System.currentTimeMillis());

它对我没有帮助,但可能对其他人有帮助。

--钱德

关于java - Android:管理多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3465230/

相关文章:

Java/Spring Boot Web 应用程序。插入带有自动递增 id 列的新行

android - 将 user-agent 中的 appname 替换为 volley

android - 如何在选项卡 fragment 中添加 ListView

c# - 如何正确安装 Facebook SDK Unity 插件?

Android GraphView 项目因实时更新而卡住

java - 没有类型为 'org.springframework.cloud.bootstrap.encrypt.RsaProperties' 的合格 bean

Java字符串文本规范化重复韩文字符

java - 为什么我不能在 Pixel 2 上设置 wifi 感知网络

java - 从包含空行的文件中读取

Android Studio 分析器 : Selected region does not have full tracking