2016-08-16 33 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消息的数据部分。

对我来说不幸的是,这个解决方案还不够,因为我无法在我工作的地方更改所有的数据结构,但我将来一定会记住这一点。

相关问题