2016-11-21 99 views
7

我正在使用Firebase雲消息傳遞將數據消息傳遞到正在開發的應用程序。根據FCM文檔,當應用程序處於前景時:Firebase雲消息傳遞 - Android:關閉應用時的通知的點擊操作不起作用

客戶端應用程序在onMessageReceived()中接收數據消息,並可以相應地處理鍵值對。

它工作正常。當應用程序在後臺,該行爲是不同的:

數據有效載荷可以用於啓動你的活動意圖進行檢索。

而且它似乎不能很好地工作。 我在通知JSON中使用了一個「click_action」參數,其中我指定了我要打開的「activity」標記內的Android Manifest中使用的intent過濾器的名稱。例如,我的通知可能是:

{ 
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", 
    "notification" : { 
     "body" : "This is a test", 
     "title" : "Test", 
     "click_action" : "com.test.click" 
    }, 
    "data" : { 
     "key" : "data" 
    } 
} 

而且我的表現有這個意圖過濾器,在「MainActivity」:

<intent-filter> 
    <action android:name="com.test.click" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

當我的應用程序在後臺,我啓動的通知,一切都似乎運作良好:我單擊通知並顯示正確的活動,並且數據密鑰位於收到的意向的「附加」中。 當我的應用程序關閉時出現問題:當我點擊通知時,應用程序正確啓動並使用「com.test.click」意圖過濾器打開活動,但是如果我嘗試發送另一個通知,沒有任何反應,並且在我重新啓動應用程序之前,後臺通知不再有效。

更清楚:當我的應用程序處於前景或背景時,通知可以正常工作。如果我的應用已關閉,則會正確處理第一個通知,但是在重新啓動應用之前,其他所有後臺通知都不起作用:如果我點按通知,我的應用會顯示最後一次觀看的活動,就好像我從中選擇了應用「最近的應用程序」菜單。如果我關閉並重新啓動應用程序,通知將恢復正常工作。

這是Android中的數據消息還是FCM問題?有沒有辦法解決這個問題?

我正在使用Nexus 5 API 23進行測試。

+1

你好,你有解決這個問題嗎? – Bahu

+0

@Bahu不幸的是,我找到了一個只使用數據信息的解決方案,使用系統托盤通知這個錯誤依然存在。我認爲這是一個FCM錯誤。你是否嘗試過我在我的問題中描述的同樣的錯誤? –

+1

是的兄弟,我試圖解決這個問題。如果您對「數據消息,使用系統托盤通知」以外的任何建議請告訴我 – Bahu

回答

8

我會盡力來形容它是如何工作的我來說,

通知有效載荷例如:

{ 
"notification": { 
    "body": "This is a test message", 
    "title": "Message", 
    "click_action" : "OPEN_ACTIVITY_1" 
}, 
"data": { 
    "key": "test key" 

} 

清單在活動

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

當應用程序在後臺或關閉FCM將創建一個「通知消息」,onClick會打開正確的活動,然後在應用程序關閉時處理通知,在onCreat中調用getIntent() e()方法

例如。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_hub); 

    handleIntents(getIntent()); //for simplicity I'm passing the value forward 


} 

當應用程序是我創建的方法的自定義通知前臺onMessageReceived我FirebaseMessagingService。

例如。

Intent intent = new Intent(this, HubActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("url", remoteMessage.getData().get("url")); 
    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_launcher) 
      .setContentTitle("Message") 
      .setContentText("This is a test message") 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

並在活動中適當地處理它我重寫方法onNewIntent(Intent intent)。

例如。

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    handleIntents(intent); //for simplicity I'm passing the value foward 


} 

所以我認爲你缺少的是onNewIntent(意向意圖),因爲已創建活動時,該處理的新意圖。

相關問題