java - Android:当应用程序未运行或在后台时将 Firebase 推送通知保存到磁盘

标签 java android firebase background gson

我在使用 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/

相关文章:

java - 多态性在 Java 中的方法参数中不起作用

java - 如何在各自的测试文件夹中为每个测试生成 cucumber.json (我使用的是 Cucumber 5,它使用 TestNG 进行并行执行)

java - 如何成功地结合这两个 JUnit 测试的逻辑?

android - 错误 :Failed to open zip file. Gradle 依赖项缓存可能已损坏(这有时会在网络连接超时后发生。)

android - 将数组从 Activity 传递到 Android View

java - Firebase 身份验证要求更新 Google Play 服务

android - 获取当前用户 Firebase Android

java - 如何通过 OSB(Oracle Service Bus)将 BLOB 类型数据(ByteArray)传递到 DB(具有 BLOB 类型列的数据库中的表)

java - 快速写入大文本文件

javascript - 检查注册时的 firestore 身份验证中是否已存在电子邮件(Angular)