【问题标题】:How can I create multiple grouped notifications with individual pending intents如何使用单独的待处理意图创建多个分组通知
【发布时间】:2020-06-24 13:46:22
【问题描述】:

所以我遇到的问题是带有待处理意图的通知没有共享正确的意图额外内容,这反过来又影响了我的应用。

我们的目标是能够创建多个分组通知以及它们的自己的待处理意图。我了解并非所有通知都需要意图,但在我的情况下,单击的每个通知都会指向应用程序中的特定区域。

我已尝试更改意图标志,但仍然遇到同样的问题。

通知创建:

PendingIntent dismissIntent = createDismissIntent(uniqueID, notification.getGroup());

notificationBuilder.setSmallIcon(smallIcon)
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getMessage())
            //.setContentIntent(contentIntent)
            .setDeleteIntent(dismissIntent)
            .setColor(context.getColor(R.color.notificationColor))
            .setVisibility(visibility)
            .setPriority(priority)
            .setStyle(style)
            .setAutoCancel(true)
            .setGroup(notification.getGroup())
            .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);

    notificationManager.notify(uniqueID, notificationBuilder.build());

方法

private PendingIntent createDismissIntent(int uniqueID, String group) {
    Intent intent = new Intent(context, ActionReceiver.class).setAction(Constants.NotificationDismissIntent);
    intent.putExtra("id", uniqueID);
    intent.putExtra("group", group);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

接收者

        int uniqueID = intent.getIntExtra("id", 0);
        String group = intent.getStringExtra("group");

        if (intent.getAction().matches(Constants.NotificationDismissIntent)) {
            try {
                if (!intent.getBooleanExtra("isSummary", false)) {
                    boolean success = NotificationStorage.getInstance(context).removeNotification(group, uniqueID);
                    if (!success)
                        Log.d(FIREBASE_MESSAGING_TAG, "Failed to remove notification from storage");
                }
            }
        }

谢谢:)

【问题讨论】:

    标签: android firebase push-notification notifications firebase-cloud-messaging


    【解决方案1】:

    取自待定意向文档 https://developer.android.com/reference/android/app/PendingIntent

    PendingIntent 本身只是对系统维护的令牌的引用,该令牌描述了用于检索它的原始数据。这意味着,即使它拥有的应用程序的进程被杀死,PendingIntent 本身仍可用于其他已被赋予它的进程。如果创建应用程序稍后重新检索相同类型的 PendingIntent(相同的操作、相同的 Intent 操作、数据、类别和组件以及相同的标志),它将接收表示相同令牌的 PendingIntent,如果它仍然有效,并且可以因此调用 cancel() 将其删除。

    由于这种行为,重要的是要知道两个 Intent 何时被认为是相同的,以便检索 PendingIntent。 人们常犯的一个错误是创建多个 PendingIntent 对象,其 Intent 仅在其“额外”内容上有所不同,期望每次都获得不同的 PendingIntent。 这不会发生。 Intent 中用于匹配的部分与 Intent#filterEquals(Intent) 定义的部分相同。如果您使用两个根据 Intent#filterEquals(Intent) 等效的 Intent 对象,那么您将获得相同的 PendingIntent。

    答案:

    有两种典型的处理方法。

    如果您确实需要同时激活多个不同的 PendingIntent 对象(例如用作同时显示的两个通知),那么您需要确保有一些不同之处它们 将它们与不同的 PendingIntent 相关联。这可能是 Intent#filterEquals(Intent) 考虑的任何 Intent 属性,或者提供给 getActivity(Context, int, Intent, int)、getActivities(Context, int, Intent[], int)、getBroadcast( Context, int, Intent, int) 或 getService(Context, int, Intent, int)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多