首页 > 解决方案 > 通知中的意图不会改变

问题描述

我有一个警报接收器,它会进行一些检查,然后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);
}

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

标签: androidnotificationsandroid-pendingintent

解决方案


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

试试下面的代码

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

第二个论点是requestCode它应该是唯一的。


推荐阅读