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

标签 android firebase-cloud-messaging

我已将 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/

相关文章:

android - 我的应用程序可以与默认的 android Gallery - Like Picasa 集成吗?

javascript - 在我的 cordova 应用程序上使用 webrtc 代码

使用 Intent 的 Android 多个电子邮件附件

java - firebase token 未生成

firebase - FCM可以在Electron App中推送通知吗

android - Firebase 云消息传递 (FCM) 不适用于 kitkat (4.4) 以下的 android 版本

java - RxAndroid 链式操作

android - 最新的 Android Studio 2020.3.1 在签名 APK 时未显示签名版本选项

ios - 是否可以在 IOS(Swift 或 Obj-C)中仅显示 Firebase 推送通知的角标(Badge),而不显示警报?

php - 从服务器端向 Android 设备发送 FCM 消息