【问题标题】:Android: Manage multiple push notification in device of an appAndroid:在应用程序的设备中管理多个推送通知
【发布时间】:2014-05-16 12:03:34
【问题描述】:

我正在开发一个实现推送通知功能的应用程序。 我的 onMessage 代码 - GCMIntentService.java 是:

@Override
protected void onMessage(Context context, Intent data) {
    String message = data.getExtras().getString("message");

    displayMessage(context, message);
    generateNotification(context, message);
}

以及generateNotification的代码-

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);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, GCMMessageView.class);

    notificationIntent.putExtra("message", message);
    // 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;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

此代码运行良好。我收到推送消息和通知。

=>但是当我发送多于一条消息时,通知就会被覆盖。而且我只能在状态栏中看到最后一条通知。

应用程序的需要是 - 如果我发送多个通知,那么所有通知都应该显示在状态栏中。

所以请指导我在我的代码中应该怎么做。

【问题讨论】:

    标签: android notifications push-notification android-notifications android-notification-bar


    【解决方案1】:

    尝试将不同的通知 id(即 nid)放入 Intent 并通知每个新通知而不是 0,这将防止过度写入您的通知

      PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0);
    

    notificationManager.notify(nid, notification);

    希望对你有帮助

    【讨论】:

    • 你拯救了我的一天。当发送了多个推送通知时,每个通知只执行最后一个待处理的意图。在我将 0 更改为 notificationId 后,它已修复。
    【解决方案2】:

    获取随机数:

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
    

    代替:

    notificationManager.notify(0, notification);
    

    使用:

    notificationManager.notify(m, notification);
    

    【讨论】:

      【解决方案3】:

      当您在中触发Notification. 时,您需要更新您的Notification ID

      notificationManager.notify(ID, notification);
      

      怎么做?

      要设置通知以使其不同,请通过调用NotificationManager.notify(ID, notification). 使用通知 ID 发出通知要在发出通知后更改此通知,请创建 NotificationCompat.Builder 对象,从以下位置构建 Notification 对象它,并发出 NotificationDifferent ID 你以前没有使用过。

      如果之前的通知仍然可见,系统将根据Notification object 的内容对其进行更新。如果先前的通知已被解除,则会创建一个新通知。

      更多信息请到官方docs

      【讨论】:

        【解决方案4】:

        您好,您需要在 notify 方法中传递唯一的通知 ID。 下面是传递唯一通知 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.
        

        如果您需要更多详细信息或有任何疑问,请告诉我。 :)

        【讨论】:

          【解决方案5】:
          notificationManager.notify(Different_id_everytime, notification);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-04
            相关资源
            最近更新 更多