2016-08-16 38 views
1

我在保存某些obj時遇到問題從FireBase推送通知系統開始使用GSon庫創建磁盤。Android:在應用程序未運行或處於後臺時將Firebase推送通知保存到磁盤

我想要做什麼: 當收到推送通知時,會創建一個obj,並將其添加到容器並保存到磁盤。它將被GUI讀取並用於創建類似通知列表的Facebook。 只有如果應用程序未運行或處於後臺,托盤中還會顯示通知圖標。

我已經有: 所有的作品,如果應用程序在前臺運行。如果應用程序未運行或處於後臺,通知圖標出現在托盤上,但不執行保存,並且當我打開應用程序時,GUI中不顯示任何內容。

這裏是我的onMessageReceived從火力

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    NotificationsContainer notificationsContainer; 

    // TODO(developer): Handle FCM messages here. 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     PixNotifications.sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle()); //Here the app show the icon in system tray, only if it's running in background. 
    } 

    notificationsContainer = Utilities.readNotificationsFromDisk(); //Read actual container from disk 
    if(notificationsContainer == null){ //No container? 
     notificationsContainer = new NotificationsContainer(); //Create an empty one 
    } 

    try{ 
     Notification notification = new Notification(remoteMessage); //Create a displayable notification 
     notificationsContainer.pushNotification(notification); //Put it in the container 
     Utilities.saveNotificationsToDisk(notificationsContainer); //Save container to disk 
     PixideCore.getInstance().reloadNotificationsContainerContent(); //Reload container in PixideCore 
    } catch (Exception e){ 
     Log.d(TAG, "Notification save to disk error"); 
    } 
} 

感謝您的幫助!

回答

0

正如FCM在提到Documentation當你的應用程序是在後臺的功能OnMessageReceived在服務不會被調用INT首位。


在背景執行應用

當你的應用是在後臺手柄通知消息,機器人指示通知消息>到系統托盤。用戶點擊通知會默認打開應用啓動器 。

這包括包含通知和數據有效負載 (以及通知控制檯發送的所有消息)的消息。在這些情況下, 通知將傳送到設備的系統托盤,並且數據>有效負載將以您的啓動器 活動的附加內容傳送。


知道,有一個相當簡單的解決方案來改變通知結構,並刪除所述通知信息,並將它傳遞在Remote消息的數據部分。

對我來說不幸的是,這個解決方案還不夠,因爲我無法在我工作的地方更改所有的數據結構,但我將來一定會記住這一點。

相關問題