【发布时间】:2014-02-11 06:10:04
【问题描述】:
下面是我的代码,它会在新消息到达我的应用程序时生成通知,
代码
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
int counter = 0;
counter++;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MessageActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(counter, notification);
}
当我收到多条消息时,会有多个通知。
例如
假设我收到 3 条新消息,则有 3 条单独的通知。但我只想要多条消息的单一通知。当我收到多条消息时,通知应该会更新,正如我们在 WhatsApp 中看到的那样。我该怎么做?
【问题讨论】:
-
为您的通知设置唯一 ID。
-
@MD:你的意思是在这一行中 notificationManager.notify(counter, notification); ???
-
您的计数器值每次都会发生变化,但您可以使用唯一 ID 来解决此问题。我的意思是唯一的 int 值
-
你是 f9 哥们,但我希望上一条消息应该在那里......
-
顺便说一句,你的问题很好。祝你好运......
标签: android android-notifications