2016-07-12 56 views
2

我正在通過控制檯將消息發送到我的應用程序,並且我設法在應用程序處於後臺時響應該應用程序。所以基本上有一個SDK顯示的通知,當用戶點擊它並且應用程序啓動時,我有一些自定義數據字段用於構建對話消息。onMessageReceived上的Firebase通知顯示與背景中的應用程序相同的通知

因爲我也想在應用上的前景時,我已經添加的服務,我已經注意到,RemoteMessage還包含一個getNotification()方法,它返回RemoteMessage.Notification我估計是使用的SDK,顯示通知時反應app在背景中。

有沒有簡單的方法來簡單地使用檢索到的通知並顯示它,就像應用程序在後臺時一樣?

+0

[this](http://stackoverflow.com/a/37845174/5296734)可能適合你! –

回答

2

確實有。只是覆蓋FirebaseMessagingService.onMessageReceived並獲得通知身體從火力地堡雲端通訊文檔這個例子:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO(developer): Handle FCM messages here. 
    // If the application is in the foreground handle both data and notification messages here. 
    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
} 

receiving downstream messages on Android下「覆蓋onMessageReceived」的火力地堡雲端通訊文檔。

+0

另外,一旦您收到該消息,則由您來生成通知。請參閱FCM Android示例中的sendNotification方法:https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService .java#L57 –

+2

謝謝,我一直希望有一個更簡單的方法,就像onMessageReceived內部一樣,只需將通知顯示出來,就像SDK在後臺應用所有自定義字段和圖標和消息時所做的那樣。由於沒有這樣簡單的方法,我使用答案中的批註並執行我自己的通知,最終將與SDK生成的通知類似。 – Alin

+0

有人可以解決這個問題嗎? http://stackoverflow.com/questions/41750548/show-notification-of-firebase-message-onrecieved-when-app-is-in-background-throu –

相關問題