英文:
Inconsistent value received in Activity after clicking on notification
问题
我遇到了一个问题,我正在尝试通过意图额外传递一个值从通知到一个活动,但是在点击通知并在活动的onCreate方法中访问该值时,接收到的值与我最初发送的值不同。我正在寻求帮助以理解和解决这个问题。
这里是负责创建通知和传递值的相关代码片段:
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CURRENT_CHAT_EXTRA, chatNotifications.getTopic());
PendingIntent actionIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AppController.CHAT_CHANNEL)
.setContentIntent(actionIntent);
在ChatActivity类中,我在onCreate方法中检索值如下:
topic = getIntent().getStringExtra(CURRENT_CHAT_EXTRA);
问题在于topic最终得到的值与最初发送的值不同。
为什么会出现这种差异,我该如何解决它?
英文:
I have encountered an issue where I'm trying to pass a value from a notification to an Activity using an intent extra. However, upon clicking on the notification and accessing the value in the Activity's onCreate method, the received value differs from the one I originally sent. I'm seeking assistance in understanding and resolving this problem.
Here's the relevant code snippet responsible for creating the notification and passing the value:
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CURRENT_CHAT_EXTRA, chatNotifications.getTopic());
PendingIntent actionIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AppController.CHAT_CHANNEL)
.setContentIntent(actionIntent);
In the ChatActivity class, I'm retrieving the value in the onCreate method as follows:
topic = getIntent().getStringExtra(CURRENT_CHAT_EXTRA);
The issue is that topic ends up with a different value than what was originally sent.
Why might this discrepancy be occurring and how can I resolve it?
答案1
得分: 1
看起来我遇到了一个问题,即在您的Activity中接收到的值与最初通过通知的intent extra发送的值不同。为了解决这个问题,您可以为每个PendingIntent使用唯一的requestCode,以确保正确接收intent extras。
您可以按照以下方式修改您的代码:
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CURRENT_CHAT_EXTRA, chatNotifications.getTopic());
// 根据主题生成一个唯一的requestCode
int requestCode = chatNotifications.getTopic().hashCode();
PendingIntent actionIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AppController.CHAT_CHANNEL)
.setContentIntent(actionIntent);
通过使用chatNotifications.getTopic().hashCode()
作为requestCode,您为每个PendingIntent创建了一个唯一的标识符。这确保每次通知点击都会创建一个单独的PendingIntent,并且intent extra将在您的Activity的onCreate方法中正确接收。
请确保相应地调整ChatActivity类中的接收代码:
topic = getIntent().getStringExtra(ChatActivity.CURRENT_CHAT_EXTRA);
通过实施这种方法,您应该能够解决在Activity中接收到与最初从通知中发送的值不同的问题。
英文:
It appears that I am encountering this issue where the value received in your Activity differs from the one originally sent via the notification's intent extra. To resolve this problem, you can make use of a unique requestCode for each PendingIntent to ensure that the intent extras are received correctly.
You can modify your code as follows:
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CURRENT_CHAT_EXTRA, chatNotifications.getTopic());
// Generate a unique requestCode based on the topic
int requestCode = chatNotifications.getTopic().hashCode();
PendingIntent actionIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AppController.CHAT_CHANNEL)
.setContentIntent(actionIntent);
By using chatNotifications.getTopic().hashCode() as the requestCode, you create a unique identifier for each PendingIntent. This ensures that each notification click will create a separate PendingIntent and the intent extra will be correctly received in your Activity's onCreate method.
Make sure to adjust your receiving code in the ChatActivity class accordingly:
topic = getIntent().getStringExtra(ChatActivity.CURRENT_CHAT_EXTRA);
By implementing this approach, you should be able to resolve the issue of receiving different values in your Activity compared to what was originally sent from the notification.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论