首页 > 解决方案 > 如何在应用程序关闭时显示 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)

我想要的是在应用程序关闭时显示此推送通知。

我看到了一些博客,但直到现在,我都找不到解决方案。

任何帮助表示赞赏。

谢谢。

标签: androidiosreact-nativegoogle-cloud-firestorereact-native-push-notification

解决方案


您可以使用 Firebase 云消息传递,即使您的应用关闭也能正常工作。但是,如果有人更改数据,因此应用程序在关闭后终止所有活动是不可能的。因此,您需要使用 Headless js 在后台调用您的任务。但为了应用程序的效率,您可以考虑 react-native-backgound-fetch。然后在后台调用您的任务。

以下是您的操作方法:

第 1 步 - 安装https://www.npmjs.com/package/react-native-background-fetch
第 2 步 - 创建无头 js
第 3 步 - 将您的任务初始化到其中(如上述包中的文档所示)


推荐阅读