React Native - 如何从推送通知打开路线

Jac*_*cka 13 push-notification reactjs react-native react-navigation

我正在使用react-navigation和react-native-push-notification.如何StackNavigator's在onNotification回调中打开某个屏幕?应该工作时:

  • 应用已关闭
  • 应用程序在前台
  • 应用程序在后台

我现在只需要在Android上工作.

我试图将回调函数传递给我的组件中的通知:

_handleClick() {
  PushNotification.localNotification({
    foreground: false
    userInteraction: false
    message: 'My Notification Message'
    onOpen: () => { this.props.navigation.navigate("OtherScreen") },
  })
}
Run Code Online (Sandbox Code Playgroud)

并onOpen在PushNotification配置中触发:

onNotification: function(notification) {
   notification.onOpen()
}
Run Code Online (Sandbox Code Playgroud)

但似乎函数不能传递给通知,除非值是一个被忽略的字符串,导致onOpen未定义.

Jac*_*cka 7

好的,好像我要发布自己的解决方案了:)

// src/services/push-notification.js
const PushNotification = require('react-native-push-notification')

export function setupPushNotification(handleNotification) {
  PushNotification.configure({

      onNotification: function(notification) {
        handleNotification(notification)
      },

      popInitialNotification: true,
      requestPermissions: true,
  })

  return PushNotification
}


// Some notification-scheduling component
import {setupPushNotification} from "src/services/push-notification"

class SomeComponent extends PureComponent {

  componentDidMount() {
    this.pushNotification = setupPushNotification(this._handleNotificationOpen)
  }

  _handleNotificationOpen = () => {
    const {navigate} = this.props.navigation
    navigate("SomeOtherScreen")
  }

  _handlePress = () => {
    this.pushNotification.localNotificationSchedule({
      message: 'Some message',
      date: new Date(Date.now() + (10 * 1000)), // to schedule it in 10 secs in my case
    })

  }

  render() {
    // use _handlePress function somewhere to schedule notification
  }

}
Run Code Online (Sandbox Code Playgroud)

  • 我建议阅读以下内容:https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html (2认同)

Anm*_*rma 7

我在 Firebase 的官方网站上找到了这个解决方案,这似乎是最好的示例/示例工作。下面是示例代码段以及附加的链接。希望它能帮助别人。

import React, { useState, useEffect } from 'react';
import messaging from '@react-native-firebase/messaging';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

function App() {
  const navigation = useNavigation();
  const [loading, setLoading] = useState(true);
  const [initialRoute, setInitialRoute] = useState('Home');

  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);
      });
  }, []);

  if (loading) {
    return null;
  }

  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName={initialRoute}>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Settings" component={SettingsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
Run Code Online (Sandbox Code Playgroud)

链接:https : //rnfirebase.io/messaging/notifications#handling-interaction