【问题标题】:Background Notification when something changes with an app React Native当应用程序发生变化时的后台通知 React Native
【发布时间】:2023-03-04 11:37:01
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    你在使用react-native-firebase 库吗?如果不是,我强烈推荐它。 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的文档

    【讨论】:

    • 我实际上正在使用 react native firebase,但我试图在每次将某些内容添加到应用程序时显示通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多