我在保存某些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");
}
}
感謝您的幫助!