简体   繁体   English

Android:管理多个通知

[英]Android: Managing Multiple Notifications

I am trying to create multiple notifications in my application. 我正在尝试在我的应用程序中创建多个通知。 To identify each notification uniquely, i have given them an unique identificationId. 为了唯一地识别每个通知,我给了他们一个唯一的identifyId。 Following is my code: 以下是我的代码:

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

Problem: When a notification is selected, Tabs activity is called passing the intent. 问题:选择通知时,会调用Tabs活动传递意图。 I want to access the unique notificationId of the notification that was selected in Tabs. 我想访问在选项卡中选择的通知的唯一notificationId。 I tried intent.putExtra() to save the notificationId in the intent. 我尝试使用intent.putExtra()将notificationId保存在intent中。 But, for multiple notifications its overwriting the notificationId and returns the latest one. 但是,对于多个通知,它会覆盖notificationId并返回最新的通知。 I dont understand as to why this is happening and how can i avoid this overwriting of notificationId. 我不明白为什么会发生这种情况,我怎么能避免覆盖notificationId。

Thanks, Chander 谢谢,Chander

I got the answer. 我得到了答案。 The intents were getting cached in. To, make a new intent, just add the following piece of code: 意图被缓存。要创建新意图,只需添加以下代码:

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

This makes the intent unique. 这使得意图独特。

Also, someone suggested me the following piece of code to make the intent unique: 此外,有人建议我使用以下代码来使意图独特:

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

It didnt help me, but might be of help to someone else. 它没有帮助我,但可能对其他人有所帮助。

--Chander --Chander

just place the notificationId instead of 0 in pendingIntent . 只需在pendingIntent放置notificationId而不是0。

PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT));

The user must also update the following code together with the above one to get it working. 用户还必须将以下代码与上面的代码一起更新以使其正常工作。

 mNotificationManager.notify(notificationId, mBuilder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM