Android 应用程序被终止或处于后台时FCM导航不工作。
我已将FCM集成到我的应用程序中,我可以在应用程序运行或终止时收到通知等。但如果应用程序正在运行,则我可以浏览特定屏幕。但是如果应用程序被终止或关闭,那么如果我点击了通知,那么它总是重定向到主屏幕,而不是导航区域。 这是我使用的代码:Android 应用程序被终止或处于后台时FCM导航不工作。,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我已将FCM集成到我的应用程序中,我可以在应用程序运行或终止时收到通知等。但如果应用程序正在运行,则我可以浏览特定屏幕。但是如果应用程序被终止或关闭,那么如果我点击了通知,那么它总是重定向到主屏幕,而不是导航区域。 这是我使用的代码: @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Check if message contains a data payload. // if
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
// if (remoteMessage.getData().size() > 0) {
L.d(TAG, "Message data payload: " + remoteMessage.getData());
// if (remoteMessage.getNotification() != null) {
String msg = remoteMessage.getNotification().getBody();
if (!TextUtils.isEmpty(msg)) {
sendNotification(remoteMessage.getData(), msg);
}
}
private void sendNotification(Map<String, String> data, String messageBody) {
String referenceKey = data.get("ReferenceKey");
String referenceValue = data.get("ReferenceValue");
switch (referenceKey) {
case Repository.ModuleCode.BRAND:
intent = new Intent(this, WebViewActivity.class);
intent.putExtra("ID", referenceValue);
intent.putExtra("browser", false);
break;
case Repository.ModuleCode.NEWS:
intent = new Intent(this, NewDetailActivity.class);
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
如果应用程序仅终止或关闭案例,我无法浏览确切页面。
提前感谢它正在按预期工作..onMessageReceived不会在应用程序被终止或处于后台时被触发。如果您的应用程序处于后台或已关闭,则通知中心会显示一条通知消息,来自该消息的任何数据都会传递给用户点击通知后启动的意图 您可以使用
getIntent().getExtras()代码>用于在启动获取意图时获取意图
更多信息
例如:
将此代码放在你的启动程序活动中。即使应用程序被终止或处于后台,这也会将你导航到预期的活动
或
如果你的应用程序在后台,你可以通过调用rest service api for firebase messaging来调用你的定制活动,如下所示。最后,我从后端更改了负载,然后它解决了我的问题
以前我是这样使用有效载荷的
{ "notification": {
"text": "Your Text1"
},
"data": {
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
然后我删除通知,并像这样使用
{
"data": {
"Message": "Test",
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
然后它在前台工作/停止/关闭
在此之后,我无法在小米笔记中得到通知 onMessageReceived仅在应用程序位于前台时才起作用,如果应用程序位于后台或已终止,则通知将以标题和消息正文显示。如果单击通知,它将引导您进入apps launcher活动,并且可以从onCreate中的getIntent检索通知的数据对象
数据的对象键/值已经被逐级插入到意图中,因此只需使用键/变量从数据对象中提取数据即可
例如:
Bundle extras = getIntent().getExtras();
String msg = extras.getString("time");
注意,只有在单击通知时,您才能获取数据。我已经完成了上述代码,并且它对我也有效。。。我可以如期得到通知。但我的情况是,如果我的应用程序关闭或被杀。然后我可以得到通知。但若我点击那个通知,那个么它并没有重定向到预期页面。但如果应用程序正在运行,则它将导航到预期页面。@AngelJannie而不重定向到预期页面的原因是“onMessageReceived”中未接收到通知数据。您正在从“onMessageReceived”重定向到预期页面。你也在你的启动程序页面上做了同样的事情…好的。我会这样做并进行测试,但如果应用程序正在运行,那么我可以在通知栏中获得通知。但是,如果我单击任何通知,那么它只会重定向到最近收到的项目。然后,如果我单击其他通知图标,它将不会调用任何东西,您对此问题有何建议???@AngelJanniee请尝试我的eg:code in launcher活动。我已添加了代码,我还记录了详细信息。如果我单击最新通知,它将显示以前的ID和密钥。然后,如果我单击其他通知图标,它将不会发生。通过手动或从程序中启用自动启动权限检查此链接。我们能够跟踪此通知并使用此方法进行分析吗?
{ "notification": {
"text": "Your Text1"
},
"data": {
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
{
"data": {
"Message": "Test",
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
Bundle extras = getIntent().getExtras();
String msg = extras.getString("time");