Intent extras is null always
Intent extras is null always
我正在使用FCM进行推送通知,并且将数据从通知发送到我打开的活动中。
构建通知的代码:
private void handleNotification(RemoteMessage remoteMessage) { String notTitle = remoteMessage.getNotification().getTitle(); String notBody = remoteMessage.getNotification().getBody(); Intent resultIntent = new Intent(this, HomeActivity.class); resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); resultIntent.putExtra("pushNotClick", "yes"); resultIntent.putExtra("pushNotHead", ""+notTitle); PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setSmallIcon(R.drawable.fb_icon); mBuilder.setColor(getResources().getColor(R.color.colorPrimary)); mBuilder.setContentTitle(notBody) .setContentText(notTitle) .setAutoCancel(true) .setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance); notificationChannel.enableLights(true); assert mNotificationManager != null; mBuilder.setSmallIcon(R.mipmap.icon_not); mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID); mNotificationManager.createNotificationChannel(notificationChannel); } assert mNotificationManager != null; mNotificationManager.notify((int) System.currentTimeMillis() /* Request Code */, mBuilder.build()); }
我在活动中获取意图的额外信息如下:
String notState = getIntent().getStringExtra("pushNotClick"); String notHead = getIntent().getStringExtra("pushNotHead");
但问题是,每次在活动中意图的额外信息都为空,我已经检查了社区中找到的所有可能的原因,但每次的响应都是相同的。
我尝试了以下链接
Android Notification PendingIntent Extras null
Always getting data null from notification intent android
我不确定我在哪里出错了。
(Intent extras is null always)这个问题出现的原因是在onCreate()方法中检查intent时,可能出现extras为空的情况。解决方法是在onNewIntent()方法中检查intent,这样即使activity已经在运行,也能够获取到intent的extras。如果activity未运行,则需要通过点击通知栏中的云消息来启动activity,并开始activity的生命周期。
具体解决方法如下:
1. 在Activity中重写onNewIntent()方法。
2. 在onNewIntent()方法中,通过intent对象获取extras。
代码示例:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Bundle extras = intent.getExtras(); // 处理获取到的extras }
如果应用程序关闭时通过通知栏传递了云消息,则根据你的代码,点击通知后会启动activity,并开始activity的生命周期。这就是PendingIntent的工作原理。
通过在onNewIntent()方法中检查intent,即使activity已经在运行,也能够获取到intent的extras。这样就可以解决(Intent extras is null always)这个问题。
问题原因:Intent的extras为空的原因是因为数据没有正确地传递给活动。
解决方法:改变发送Intent extras的方式,使用bundle来发送数据。
以下是详细的解决步骤:
1. 首先,确认数据是否正确地附加到Intent中。可以使用以下代码来附加数据到Intent中:
Intent intent = new Intent(this, YourActivity.class); intent.putExtra("key", "value"); startActivity(intent);
2. 确保在接收Intent的活动中正确地获取数据。可以使用以下代码来获取Intent extras中的数据:
Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("key"); // 使用数据进行后续操作 } else { // 处理数据为空的情况 }
3. 如果上述步骤仍然无法解决问题,可以尝试使用Bundle来发送数据。可以使用以下代码来发送带有Bundle的Intent:
Intent intent = new Intent(this, YourActivity.class); Bundle bundle = new Bundle(); bundle.putString("key", "value"); intent.putExtras(bundle); startActivity(intent);
4. 确保在接收Intent的活动中正确地获取Bundle中的数据。可以使用以下代码来获取Bundle中的数据:
Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("key"); // 使用数据进行后续操作 } else { // 处理数据为空的情况 }
通过改变发送Intent extras的方式,使用Bundle来发送数据,可以解决Intent extras为空的问题。这种方式可以确保数据正确地传递给活动,并且可以避免Intent extras为空的情况。
从上述内容中可以得出以下结论:
问题原因:Intent extras为空的原因是因为数据被包含在RemoteMessage的data字段中,而不是Intent的extras字段中。
解决方法:在处理消息的代码中,需要检查RemoteMessage的data字段是否包含数据。如果包含数据,则需要根据需要进行处理。可以通过以下方式进行检查和处理:
// 检查消息是否包含数据有效载荷。 if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); if (/* 检查是否需要通过长时间运行的任务处理数据 */ true) { // 对于长时间运行的任务(超过10秒),请使用WorkManager。 scheduleJob(); } else { // 在10秒内处理消息 handleNow(); } }
通过以上代码,我们可以检查RemoteMessage的data字段是否包含数据,并根据需要进行相应的处理操作。这样就可以避免Intent extras为空的问题。