应用程序本机更改时的后台通知

时间:2020-11-06 13:49:54

标签: android ios react-native react-native-push-notification

我是新来的响应本机的人,正在尝试创建一个我希望在添加一些新内容时接收后台通知的应用程序。

我已经使用Firebase云消息传递设置了后台通知,但是我注意到,这仅允许我向设备发送消息并将用户带到主屏幕。我希望当用户收到通知并单击该通知时,它将根据通知将其带到特定屏幕。

做完一些研究后,我似乎找不到任何方法可以做到这一点,有人会知道如何做到这一点,或者知道有关此问题的教程吗?

1 个答案:

答案 0 :(得分:0)

您是否正在使用react-native-firebase lib?如果没有,我强烈推荐。该API提供了两个API来处理通知交互。

  • getInitialNotification:从退出状态打开应用程序时。
  • onNotificationOpenedApp:应用程序在运行时,但在后台运行。

  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

上的文档