【问题标题】:Intent in Notification does not change通知中的意图不会改变
【发布时间】:2019-02-14 21:36:24
【问题描述】:

我有一个警报接收器,它会进行一些检查,然后为每次检查创建一个Notification。这意味着它可以创建多个Notification。这一切都很好。但是,我有一个 Intent 连接到通知,以便在点击通知时启动活动。

这是通知代码:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_OUTPUT_LEVELS)
                .setSmallIcon(icon)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(longmessage))
                .setContentIntent(getPendingIntent(solarEdge, reason))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

// notificationId is a unique int for each notification that you must define
// we use the installation's ID to make sure all notifications get sent
notificationManager.notify(solarEdge.getInfo().getId(), mBuilder.build());

创建PendingIntent的方法是:

private PendingIntent getPendingIntent(SolarEdge solarEdge, int reason) {
    String apikey = solarEdge.getApikey();
    int installationId = solarEdge.getInfo().getId();

    Intent intent = new Intent(context, InstallationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(EXTRA_API_KEY, apikey);
    intent.putExtra(EXTRA_INSTALLATION_ID, installationId);
    intent.putExtra(EXTRA_REASON, reason);

    return PendingIntent.getActivity(context, 0, intent, 0);
}

我遇到的问题是,即使我创建了不同的意图,它仍然总是使用相同的附加项(apikey 和 installid)调用Activity。它总是取第一个创建的。

【问题讨论】:

    标签: android notifications android-pendingintent


    【解决方案1】:

    问题似乎是由您传递给getActivity 方法的参数引起的。

    试试下面的代码

    PendingIntent.getActivity(context, <unique_value_per_every_call>, intent, 0);
    

    第二个参数是requestCode,所以它应该是唯一的。

    【讨论】:

    • @BartFriederichs 不客气,文档有时不包含重要信息。
    • @BartFriederichs 它是一个请求代码意味着它对于该请求应该是唯一的,但我也喜欢它。
    • 要在每次调用中获得唯一的PendingIntent,对PendingIntent.getActivity() 的调用需要或者提供唯一的请求代码唯一的@ 987654326@。要使Intent 独一无二,必须提供任一唯一的操作、数据或组件。因此有几种方法可以解决这个问题。
    【解决方案2】:

    接受的答案是解决问题的一种方法,但不是唯一的方法。

    要在每次调用时获得唯一的PendingIntent,对PendingIntent.getActivity() 的调用需要提供唯一的requestCode唯一的Intent。要使Intent 独一无二,必须提供任一唯一的操作、数据或组件。因此有几种方法可以解决这个问题。

    【讨论】:

      【解决方案3】:

      已知的“问题”/怪癖/设计,我们程序员不断遇到它。 如果意图相同(动作、数据或组件相同,不仅额外数据不同),则请求代码参数必须为 0 以外的值。

      int requestCode = 1;
      PendingIntent.getActivity(context, requestCode, intent, 0);
      

      另外:

      如果您想稍后更新通知/待处理 Intent,可以使用相同的 requestCode。

      如果您添加多个待处理的意图,它们必须都不同。示例:停止和暂停动作。

      我通常遵循这种模式:

      requestCode = 1 for the content intent.
      requestCode = 2 for first action
      requestCode = 3 for second action
      etc
      

      【讨论】:

      • 实际上每次调用都必须是唯一的。如果我只输入 1,它仍然无法正常工作。
      • 不是每次调用,而是根据您打算添加的意图。如果您想稍后更新它,例如将“播放”意图更改为“暂停”意图,您可以使用相同的。
      • 这是错误的。请求代码不需要非零。要在每次调用中获得唯一的PendingIntent,对PendingIntent.getActivity() 的调用需要 提供唯一的请求代码 唯一的Intent。要使Intent 唯一,必须提供任一唯一的操作、数据或组件。因此有几种方法可以解决这个问题,但将 requestCode 更改为“1”并不是其中之一。
      • 哦,是的,完全同意您也可以按照您的说法使意图独一无二。
      猜你喜欢
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多