Android 从通知启动应用程序时无法从意图中读取额外内容
我正在使用FCM接收消息,在onMessageReceived上我有代码Android 从通知启动应用程序时无法从意图中读取额外内容,android,android-intent,notifications,Android,Android Intent,Notifications,我正在使用FCM接收消息,在onMessageReceived上我有代码 NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Service.NOTIFICATION_SERVICE); Intent notifyIntent = new Intent(this,HelloBubblesActivity.class)
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Service.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(this,HelloBubblesActivity.class);
notifyIntent.putExtra("id",sender);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("New Message from " + name)
.setSmallIcon(R.drawable.smalllogo)
.setAutoCancel(false)
.setContentText(text)
.setContentIntent(pendingIntent);
notificationManager.notify(sender, mBuilder.build());
当应用程序从通知中打开时,我遇到了一个问题,这里的代码是额外的is-1
int chatId = getIntent().getIntExtra("id",-1);
我错过了什么吗?请确保您的活动不是单实例启用的。有一段时间,单实例活动作为FCM文档出现了这个问题 要接收消息,请使用扩展的服务 FirebaseMessagingService。您的服务应该覆盖 onMessageReceived回调,为大多数消息类型提供, 除以下例外情况外: 应用程序处于后台时发送的通知。在这个 在这种情况下,通知将发送到设备的系统托盘。A. 默认情况下,用户点击通知会打开应用程序启动器。信息 同时具有通知和数据负载。在这种情况下 通知被传送到设备的系统托盘,数据 有效载荷是按照你的发射器的意图额外交付的 活动 因此,onMessageReceived将永远不会触发,如果应用程序在后台,并且当您单击MainActivity将启动的通知时,您所需要的只是获取意图并从服务器发送额外的消息
if (null != getIntent().getExtras() )
{
chatId = getIntent().getStringExtra("sender");
}
你的
sender
是int
吗?是的,确定它是int,尝试在onNewIntent
中获得目的。我刚刚解决了它,请看我的答案。我把它放在了安卓上:launchMode=“singleTask”不是单实例,你认为这是问题吗?是的,我认为是。可能是因为Android没有真正刷新活动。它只是把它带到前面。请尝试覆盖onNewIntent(Intent)以拦截新的Intent或将android:launchMode=“singleTask”删除到您的活动中