Sam*_*mar 3 android push-notification android-pendingintent google-cloud-messaging
大家好我正在使用GCM推送通知向用户传递一些通知.我的问题是当我发送单个消息然后这个工作正常如果发送多个,然后最后一条消息显示给所有通知.帮我这个,我犯了错误......?
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, GCMMessageView.class);
String[] messageArray = message.split("\\#");
// param :: agentId, Name, Msg
DatabaseHelper db = new DatabaseHelper(context);
int notificationId = db.insertnotifications(messageArray[1], messageArray[0], messageArray[messageArray.length-1]);
notificationIntent.putExtra("message", messageArray[messageArray.length-1]);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentText(messageArray[messageArray.length-1])
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(icon)
.setWhen(when)
.setContentIntent(intent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(notificationId, notification);
}
Run Code Online (Sandbox Code Playgroud)
在您的通知中使用此编码
int requestID = (int) System.currentTimeMillis();
PendingIntent intent = PendingIntent.getActivity(context, requestID,
notificationIntent,0 );
Run Code Online (Sandbox Code Playgroud)
然后你的通知不会覆盖我希望这会对你有所帮助
| 归档时间: |
|
| 查看次数: |
2689 次 |
| 最近记录: |