【问题标题】:GCM message is getting overriddenGCM 消息被覆盖
【发布时间】:2013-12-31 11:09:46
【问题描述】:

我正在使用 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);
}

【问题讨论】:

    标签: android push-notification google-cloud-messaging android-pendingintent


    【解决方案1】:

    在您的通知中使用了此编码

      int requestID = (int) System.currentTimeMillis();
      PendingIntent intent = PendingIntent.getActivity(context, requestID,
      notificationIntent,0 );
    

    那么你的通知不会覆盖我希望这对你有帮助

    【讨论】:

    • 不确定我错过了什么,但即使按照您提到的添加当前时间也没有按预期工作。我在 notify 方法中更新了通知 ID,解决了这个问题,感谢您的帮助!
    【解决方案2】:

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

    【讨论】:

    • 如何使用 HTTP 请求?有什么想法吗?
    【解决方案3】:

    是的,同意 Dipo 的回答。根据Android Notification Developer Guide,由于多个通知的相同通知ID,这将被覆盖。所以最后一条消息正在接收端接收。您需要为每个通知实现随机唯一通知 id。有多种方法可以实现这一点。你可以用这个

    int notificationId = new Random().nextInt();
    

    Notification id也可以使用系统时间,单位是Mili Seconds。然后你需要把这个唯一的Notification ID传递给notificationManager.notify()

    干杯!继续编码

    【讨论】:

      【解决方案4】:

      回答这个帖子很晚,但觉得这对某人有帮助。同样的实现也适用于 FCM firebase 实现

      // Get a random value 
      int notificationId = new Random().nextInt();
      // Add the notification ID which is unique as a first value of the notify method.
      notificationManager.notify(notificationId, notificationBuilder.build());
      

      【讨论】:

        猜你喜欢
        • 2020-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多