首页 > 解决方案 > 当应用程序关闭时,我可以使用 React Native 获得推送通知吗?

问题描述

当应用程序关闭时,我正在尝试使用 React-Native/RNFirebase 在 IOS 上获取通知并更新启动器图标徽章。但不幸的是,RNFirebase 似乎没有任何支持。关闭应用程序时,onNotification() 侦听器似乎不起作用。(在 XCode 上启用了后台模式/推送通知)

收到通知并关闭应用程序时,是否有任何解决方法可以更新启动器图标徽章?

标签: react-nativereact-native-firebase

解决方案


如果您的应用已关闭,您可以使用以下命令检查它是否被通知打开: firebase.notifications().getInitialNotification()

这是一个使用它的示例:

const notificationOpen = await firebase
      .notifications()
      .getInitialNotification();
    if (notificationOpen && notificationOpen.notification) {
      let { title, body, data } = notificationOpen.notification;
      if (!title && !body) {
        title = data.title;
        body = data.body;
      }

      const notification: Notification = notificationOpen.notification;
      //firebase.notifications().removeDeliveredNotification(notification.notificationId);
      this.handleNotificationActions(
        title,
        body,
        data,
        navigator,
        t,
        "background"
      );
    }

推荐阅读