android - 如何在应用程序关闭时显示 React Native Push Notification
问题描述
我正在使用 React Native Push Notification(本地通知)[https://github.com/zo0r/react-native-push-notification]。
显示通知的逻辑如下所示。
我在 react-native 中使用了“onSnapshot”函数来检查 Firestore 数据是否已更改。
如果更改,我会在应用程序中显示本地通知。
...
const ref = firestore().collection('players');
return ref.onSnapshot(querySnapshot => {
querySnapshot.forEach(doc => {
....
// called push notification
PushNotification.localNotification({
channelId,
title,
message,
bigText,
});
});
});
...
当应用程序在前台或后台时,它运行良好。(安卓和iOS)
我想要的是在应用程序关闭时显示此推送通知。
我看到了一些博客,但直到现在,我都找不到解决方案。
任何帮助表示赞赏。
谢谢。
解决方案
您可以使用 Firebase 云消息传递,即使您的应用关闭也能正常工作。但是,如果有人更改数据,因此应用程序在关闭后终止所有活动是不可能的。因此,您需要使用 Headless js 在后台调用您的任务。但为了应用程序的效率,您可以考虑 react-native-backgound-fetch。然后在后台调用您的任务。
以下是您的操作方法:
第 1 步 - 安装https://www.npmjs.com/package/react-native-background-fetch
第 2 步 - 创建无头 js
第 3 步 - 将您的任务初始化到其中(如上述包中的文档所示)
推荐阅读
- .net-core - ASP.NET Core:Azure 广告登录在 iPhone (Safari) 浏览器上不起作用。在windows的其他浏览器上很好
- javascript - 使用节点js的knex返回多个“SUM”列
- python - 试图理解 PyTorch SmoothL1Loss 的实现
- java - 通过mysql数据库对Jmeter进行多用户登录的负载测试
- visual-studio-code - VS Code,在默认浏览器中打开不起作用?
- c# - 从数据网格视图和数据库中删除选定的行
- mysql - 使用 MySql 将列中的数字替换为相应的字符串
- deep-learning - GPU需要多少?
- sql - 如何在SQL中的另一个表中的两个数字之间找到表中的一个数字
- pandas - TypeError:无法从 ndarray 中减去 DatetimeArray