我在使用 GSon 库将 FireBase 推送通知系统创建的一些 obj 保存到磁盘时遇到问题。
我想做的事情: 当收到推送通知时,将创建一个 obj,将其添加到容器中并保存到磁盘。 GUI 将读取并使用它来创建类似 facebook 的通知列表。 仅如果应用未运行或处于后台,通知图标也会显示在托盘中。
我已经拥有的: 如果应用程序在前台运行,则一切正常。如果应用程序未运行或在后台,通知图标会出现在托盘上,但不会执行保存,并且当我打开应用程序时,GUI 中不会显示任何内容。
这是我从 FireBase 接收的 onMessage
@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");
}
}
感谢您的帮助!
最佳答案
正如 FCM 在其 Documentation 中提到的那样当您的应用程序在后台时,您的服务中的 OnMessageReceived 函数不会首先被调用。
<小时/><小时/>Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages > to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data > payload is delivered in the extras of the intent of your launcher Activity.
知道有一个相当简单的解决方案来更改通知结构并删除通知信息并将其传递到远程消息的数据部分。
不幸的是,对我来说,这个解决方案还不够,因为我无法更改我工作的地方的所有数据结构,但我将来一定会记住这一点。
关于java - Android:当应用程序未运行或在后台时将 Firebase 推送通知保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38980920/