2016-12-02 38 views
2

我已將FCM集成到了我的應用程序中,我可以在應用程序運行或死亡等情況下收到通知。但是如果應用程序正在運行,那麼我可以導航特定的屏幕。但是如果應用程序死亡或關閉,那麼如果我點擊了通知,那麼總是將其重定向到主屏幕而不是導航區域。 這是我使用的代碼: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> 

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

,如果應用程序被殺害或僅關閉情況下,我無法導航的確切網頁。 在此先感謝

回答

3

最後我從後端更改了Payload,然後它解決了我的問題。

以前我使用的有效載荷這樣

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

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

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

然後它的工作前景/殺死/關閉。

之後,我只能在xiaomi note3中收到通知。

+1

查看小米筆記中的網絡3.可能是因爲某些網絡錯誤.. –

+0

小米HM注意1 LTE android 4.4.4不工作.. – AngelJanniee

+0

通過手動或從程序啓用自動啓動權限勾選此鏈接https:// stackoverflow.com/questions/39368251/how-to-enable-autostart-option-for-my-app-in-xiaomi-phone-security-app-programma。 – Sandeep

2

它按預期工作..onMessageReceived不會觸發,如果應用程序被殺害或在後臺。如果您的應用程序在後臺或關閉然後通知消息顯示在通知中心,任何來自該消息的數據被傳遞給由於用戶點擊通知而啓動的意圖。

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

更多信息here;

如:

 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) 
     } 
     } 

將此代碼放在你的發射activity.And這將引導您到您的預期活動即使該應用程序被殺害或者是在後臺。

或者

您可以致電通知點擊自定義活動,如果您的應用程序在後臺通過調用火力消息REST服務API這裏https://stackoverflow.com/a/37599088/3111083給出。

+0

我已經完成了上面的代碼,它也在工作,我也可以......我可以按預期得到通知。但我的情況下,如果我的應用程序關閉或死亡。那麼我可以得到通知。但是如果我點擊那個沒有重定向到預期頁面的通知。但如果應用程序正在運行,那麼它將導航到預期的頁面。 – AngelJanniee

+0

@AngelJanniee不重定向到期望頁面的原因是因爲通知數據未收到「onMessageReceived」。您正在重定向到「onMessageReceived」的預期頁面。您也可以從您的啓動頁面執行相同的操作。 –

+0

好的。我會做和測試,但如果應用程序正在運行,那麼我可以在通知欄中獲取通知。但是如果我點擊任何通知,那麼它只會重定向到最新收到的項目。然後如果我點擊其他通知圖標,那麼它不會調用任何東西 你可以建議這個問題?? – AngelJanniee

0

onMessageReceived僅當您的應用程序位於前臺時才起作用,如果應用程序位於後臺或將其終止,則該通知將隨着標題和消息正文而膨脹。如果您點擊通知,它會引導您進入應用程序啓動器活動,並且可以從onCreate中的getIntent中檢索通知的數據對象。

數據的對象鍵/值已經成爲意圖中的serilies,因此只需從數據對象中用鍵/ varible提取數據即可。

例如:

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

注意,如果點擊了通知你只能得到這些數據。

相關問題