gpt4 book ai didi

android - FCM 导航在应用程序被终止或后台运行时不工作。

转载 作者:太空狗 更新时间:2023-10-29 16:30:21 29 4
gpt4 key购买 nike

我已将 FCM 集成到我的应用程序中,我可以在应用程序运行或终止时收到通知等。但如果应用程序正在运行,那么我可以导航特定屏幕。但是如果应用程序被终止或关闭,那么如果我单击通知,那么它总是会重定向到主屏幕而不是导航区域。这是我使用的代码:

@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());
}
}

在 list 中:

 <service
android:name=".fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">

<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

在 App Gradle 中

  compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.google.android.gms:play-services-maps:9.6.1'

如果应用程序终止或仅关闭案例,我将无法导航到确切的页面。提前致谢

最佳答案

最后我从后端更改了 Payload 然后它解决了我的问题。

之前我是这样使用PayLoad的

{ "notification": {
"text": "Your Text1"
},
"data": {
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}

然后我删除通知并像这样使用。

 {
"data": {
"Message": "Test",
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}

然后它正在为前台工作/被杀死/关闭。

此后我无法在 xiaomi note3 中收到通知。

关于android - FCM 导航在应用程序被终止或后台运行时不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929436/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com