2010-08-12 41 views
9

我想在我的应用程序中创建多个通知。为了唯一标识每个通知,我给了他们一个唯一的身份标识。以下是我的代码:Android:管理多个通知

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

问题: 当选择了一个通知,标签活动被称为合格的意图。我想要访问在选项卡中选择的通知的唯一通知ID。我尝试了intent.putExtra()来保存意图中的notificationId。但是,对于多个通知,它将覆盖notificationId并返回最新的通知。我不明白为什么会发生这种情况,我如何避免这种覆盖notificationId。

感谢, 钱德尔

回答

15

我得到了答案。的意图都拿到缓存中,创建一个新的意图,只需添加以下代码块:

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

这使得意图是独一无二的。

此外,有人建议我下面的一段代码,以使意图独特:

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

它没有帮我,但可能会有所帮助别人。

--Chander

7

只需将notificationId,而不是0 pendingIntent

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

用户还必须更新以下代码以及上面的代码以使其工作。

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