React Native - How to open route from push notification

前端 未结 3 954
花落未央
花落未央 2021-02-04 05:43

I\'m using react-navigation and react-native-push-notification. How can I open a certain StackNavigator\'s screen in onNotification<

3条回答
  •  佛祖请我去吃肉
    2021-02-04 06:17

    Okay, it seems like I gotta post my own solution :)

    // 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
      }
    
    }
    

提交回复
热议问题