簡體   English   中英

應用程序被殺死或后台運行時,FCM導航無法正常工作。

[英]FCM Navigation not working while app was killed or background.

我已將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());
}
                    }

在清單中:

 <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的

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

然后我刪除通知並像這樣使用。

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

然后它為前景工作/殺死/關閉。

此后,我無法僅在小米note3中獲得通知。

它按預期工作..onMessageReceived不會在應用程序被殺死或在后台時觸發。如果您的應用程序在后台或關閉,則通知中心將顯示一條通知消息,並且該消息中的所有數據都會傳遞到用戶點擊通知后啟動的意圖。

您可以使用getIntent().getExtras(); 在啟動以獲取意圖時獲取意圖。

更多信息在這里 ;

例如:

      Bundle bundle = getIntent().getExtras();
      if (bundle != null) {
          if (bundle.containsKey("data")) {
          Intent intent = new Intent(mContext, ExpectedActivity.Class)
          intent.putExtras("PUSH_KEY",bundle.get("data").toString());
          startActivity(intent)
        }
      }

將此代碼放在啟動器活動中,即使應用程序被殺死或處於后台也可以將您導航到預期的活動。

要么

如果您的應用程序處於后台,則可以通過單擊通知來調用自定義活動,方法是調用rest服務api來發送Firebase消息,如此處https://stackoverflow.com/a/37599088/3111083所示

onMessageReceived僅在您的應用程序位於前台時才起作用,如果該應用程序在后台或被殺死則無法正常工作,則該通知將隨標題和消息正文一起膨脹。 如果單擊通知,它將引導您轉到應用啟動器活動,並且可以從onCreate的getIntent中檢索通知的數據對象。

數據的對象鍵/值已經很有意義,因此只需從數據對象中提取帶有鍵/變量的數據即可。

例如:

Bundle extras  = getIntent().getExtras();
String msg = extras.getString("time");

請注意,只有在單擊通知后才能獲取數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM