当应用程序 React Native 发生某些变化时的后台通知

Background Notification when something changes with an app React Native

我是 React Native 的新手,我正在尝试创建一个应用程序,我希望在添加一些新内容时收到后台通知。

我已经使用 firebase 云消息传递设置了后台通知,但是我注意到这只允许我向设备发送消息并将用户带到主屏幕。我希望当用户收到通知并单击它时,它会根据通知将他们带到某个屏幕。

经过一些研究后,我似乎找不到任何方法来做到这一点,有人知道如何做到这一点或知道这方面的教程吗?

您在使用 react-native-firebase 库吗?如果不是,我强烈推荐它。 API 提供两个 API 来处理通知交互。

  • getInitialNotification: When the application is opened from a quit state.
  • onNotificationOpenedApp: When the application is running, but in the background.

  useEffect(() => {
    // Assume a message-notification contains a "type" property in the data payload of the screen to open

    messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });
  }, []);

请查看 Notification Handling Interaction Section

中的文档