【问题标题】:Add a new notification when push notification is received (not replace the previous)收到推送通知时添加新通知(不替换以前的通知)
【发布时间】:2013-06-17 12:59:20
【问题描述】:

我在我的应用程序中使用推送通知。我需要在推送通知传递时显示通知。如果我发送另一个通知(不清除之前的通知),它将替换旧通知。

这是我使用的代码:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
        contentIntent);
mNotificationManager.notify(1, notification);

但我不想替换通知,我想将其添加为新通知。

【问题讨论】:

    标签: android notifications notificationmanager


    【解决方案1】:

    您还可以使用 System.currentTimeMillis() 为您的通知分配唯一 ID。

    int id = (int) System.currentTimeMillis();
    mNotificationManager.notify(id, notification);
    

    【讨论】:

    • 这是个好主意。因为时间 id 永远不会重复。我也在php中使用相同的。谢谢。但是将您的代码更改为 notify(id, notification);它可能对其他人有用
    • 感谢 Indra 提及,现在我已将 1 替换为 id。
    • thanqu 先生 ..for 发布了它
    【解决方案2】:

    您每次都需要提供不同的 ID 作为通知 ID。最好的方法是向 GCM 发送一个 ID 字段,然后可以通过 GCMIntentService 的 onMessage() 方法中的Intent.getExtras().getInt() 访问该字段。

    如果这不可能,我建议使用(int)Math.random()*10 之类的东西来生成一个随机整数作为您的通知 ID。这将(部分)确保您的通知不会相互替换。

    【讨论】:

      【解决方案3】:

      很简单

      更改通知 ID

        mNotificationManager.notify(1, notification);
      

      而不是1

      更多信息请参考Link

      【讨论】:

        【解决方案4】:

        每次都使用新的通知 ID,而不是硬编码为 1:

        int i = x; //Generate a new integer everytime
        mNotificationManager.notify(i, notification);
        

        【讨论】:

          【解决方案5】:

          我不确定你的用例是什么,但the Android design guidelines recommend not doing it at all.

          不要: 做:

          【讨论】:

          • 完全取决于我认为的用例,现在由于通知渠道,人们可以更明确地了解这一点
          • 这很奇怪。如果您在一个应用程序中有多个不同的事件,例如聊天、订单确认、付款状态,该怎么办?
          【解决方案6】:

          我们需要生成新通知的唯一通知 ID。

          发布要在状态栏中显示的通知。如果通知与 您的应用程序已经发布了相同的ID并且尚未取消,它将被更新的信息替换。

            @param id An identifier for this notification unique within your application.
            @param notification A {@link Notification} object describing what to show the user. 
            Must not be null.
          
          public void notify(int id, Notification notification)
          {
              notify(null, id, notification);
          }
          

          例子:

          int  id =(int) System.currentTimeMillis();
                      mNotificationManager.notify(id, notify);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-01-20
            • 1970-01-01
            • 2022-06-19
            • 1970-01-01
            • 1970-01-01
            • 2021-01-28
            • 2017-12-22
            相关资源
            最近更新 更多