Android多重通知

问题描述:

我开发了一个应用程序,用户可以在其中创建事件并为该事件设置通知。所以我想添加多个通知。我正在使用下面的代码。Android多重通知

final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",calendar.getTimeInMillis()); 
Context context = getApplicationContext(); 
Intent notifyIntent = new Intent(context, ViewDoughnut.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(ViewCal.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT; 
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 

当我添加一个事件并使用上面的代码创建通知时,它工作正常。但是,如果我添加另一个事件,则不会创建新通知,而只会更新旧通知。我想添加一个通知。怎么做?此外,如果用户删除与其相对应的事件,我想删除任何特定的通知。怎么可能?

我假设SIMPLE_NOTIFICATION_ID是一个常数?要有单独的通知,您需要为每个通知使用不同的ID。

+0

但当用户删除相应的事件时如何取消任何通知?如果我用mNotificationManager.cancel(editEventid)尝试过,它根本不起作用。 – 2011-03-08 12:20:13

+0

@ user525004如果您将相同的ID传递给您在创建通知时使用的cancel方法,那么它应该可以工作(适用于我的应用程序)。如果您的活动已经拥有自己的唯一ID,请将这些用于通知ID。 – 2011-03-08 16:21:47

+0

我该如何让ID有所不同?每次通过自己创建的通知时+1,你能告诉我一些代码吗? – 2012-10-15 17:07:12

下面是通唯一的通知ID代码:

//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences. 
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0"); 
int notificationIdinInt = Integer.parseInt(notificationId); 

notificationManager.notify(notificationIdinInt, notification); 

// will increment notification id for uniqueness 
notificationIdinInt = notificationIdinInt + 1; 
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + ""); 
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences. 

让我知道如果您需要更详细的信息或任何查询。 :)