2017-09-16 96 views
0

FCM服務。 NotificationDatabaseHandler是助手類。保存消息標題和當前時間。當應用程序處於後臺時,無法成功保存FCM消息,因爲它在應用程序處於前臺時正常工作

public class ApplicationFCMService extends FirebaseMessagingService { 

    private static final String TAG = ApplicationFCMService.class.getName(); 
    private NotificationUtils notificationUtils; 
    private NotificationDatabaseHandler databaseHandler; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 

     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     databaseHandler = new NotificationDatabaseHandler(getApplicationContext()); 

     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
      handleNotification(remoteMessage.getFrom(), remoteMessage.getNotification().getBody()); 

      databaseHandler.addNotification(remoteMessage.getNotification().getBody(), getDate()); 
      // Log.d("FCM", messagesSet.toString()); 
      Log.d("FCM", remoteMessage.getNotification().getBody()); 
     } 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

      try { 
       JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
       handleDataMessage(json); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 
     } 
    } 
+0

檢查這個答案:https://stackoverflow.com/questions/45875859/fcm-onmessagereceived-not-calling-android/45880920#45880920 – Maddy

+0

https://stackoverflow.com/questions/45267335/create-an-repetitive - 當應用程序未運行時發生高度警報 - 遠程觸發/ 45406863#45406863 –

+0

有沒有任何方法將通知作爲Firebase的「數據」發送? –

回答

0

我已經使用郵遞員發送數據通知(測試)。在接收到數據消息時,會執行onMessageReceived()方法。 現在,即使應用程序處於後臺,我也可以將通知保存在數據庫中。

相關問題