GCM消息已被覆盖 [英] GCM message is getting overridden

查看:86
本文介绍了GCM消息已被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GCM推送通知将一些通知传递给用户.我的问题是,当发送一条消息时,如果发送多条消息,那么最后一条消息会显示在所有通知中,这样就可以正常工作.

I am using GCM push notification to pass some notification to the user. My problem is when am sending single message then this works fine if send more than one then the last message is shown to all the notification.

我在哪里弄错了?

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

推荐答案

为每条消息设置唯一的通知ID.因此,它已被覆盖.

Make notification id unique for each message. Due to this, it's being overridden.

这篇关于GCM消息已被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆