react-native - 5 分钟后收到应用关闭时来自 Firebase 的仅数据消息
问题描述
当应用程序被用户关闭时,我试图在我的 Android 托盘上显示一条数据消息,即当用户将应用程序拖到最近应用程序列表的一侧时。虽然,数据消息只是像这样被接收:
- 应用程序已关闭。已发送数据消息。
- 关闭应用程序时未收到消息。
- 应用程序已打开。
- 4~5 分钟后收到数据消息并显示在 Android 托盘中。
为了实现这一点,我正在使用react-native-firebase库,遵循其docs。
import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';
export default async (message: RemoteMessage) => {
const notifPromise = new Promise((resolve, reject) => {
let notification = new firebase.notifications.Notification();
notification.android.setPriority(firebase.notifications.Android.Priority.High);
notification.android.setChannelId("test-channel");
resolve(firebase.notifications().displayNotification(notification));
});
console.log("MESSAGE IN BACKGROUND OR APP CLOSED");
return notifPromise.resolve();
}
上面的代码在后台运行良好,我的意思是当应用程序只是“最小化”到辅助计划时。
AndroidManifest.xml、HeadlessTask 和 MainApllication.java 理论上是符合文档的。我只是在 Android 托盘中显示空白 UI 进行测试。
邮递员发送的消息:
{
"to": "erVxmCT6rgA:APA91bGn6q9...",
"data": {
"custom1": "custom1",
"custom2": "custom2"
}
}
问题:一旦它在后台运行会出现什么问题?为什么会发生这种行为?
解决方案
经过深入搜索,我可以弄清楚。
我在华硕设备上运行应用程序。根据这个stackoverflow的回答,华硕智能手机有一个性能技巧可以延长电池寿命,所以当你从最近的应用程序列表中刷掉一个应用程序时,你是在强制应用程序停止。因此,负责接收消息的 HeadlessJSTask 取自 android 进程列表。
我还在我的代码中进行了一些编辑:
修复承诺:
export default async (message: RemoteMessage) => {
const notification = new firebase.notifications.Notification();
notification.android.setPriority(firebase.notifications.Android.Priority.High);
notification.android.setChannelId("test-channel");
notification.setTitle(message.data.custom1);
firebase.notifications().displayNotification(notification);
return Promise.resolve(message);
}
在我们的数据消息中设置高优先级(后台和应用关闭消息是必需的):
{
"to": "cKUNOaOnvaY:APA91bFVAPLSuHogmZfxc1GlhqOhEkxcscoRvZxRrzno0XjyDkqYZVmNqJVL4v6mcQgH4p9zt9Zxz5aDugCjNy7CBg_pbXb8u8X6336K0x6WffdXoGOl50lCtHt46oS78Yyc9XM3gPJQ",
"data": {
"custom1": "custom1",
"custom2": "custom2"
},
"priority": "high"
}
推荐阅读
- ios - 在 React Native iOS Native 组件中设置 UITextView inputView 时出错
- javascript - Threejs - 灯光的坐标位置似乎仅在某些设备上反转
- file - Unix终端:将文件分成4等份
- postgresql - 使用 chrome 浏览器命令将 pgAdmin 4 作为独立应用程序启动
- ios - 当弹出窗口在屏幕上时,UIBarButtonItem 未脱色/禁用
- mysql - 多对多表 id 的最佳实践
- html - 不知道如何让我的按钮链接到另一个 html 页面:(
- sql - 如何使用在事务中创建的新表
- c# - 选择根据列表检查的已选中列表框项目
- python - 自定义应用程序指示器在 Ubuntu 中工作,但是当我从登录启动菜单将 Ubuntu 更改为 Gnome 时无法工作