React Native:通过点击推送通知来调用函数?

时间:2016-07-15 04:49:35

标签: push-notification react-native

所以我想在点击传入的推送通知时将用户放入特定的导航器场景。使用的“场景”和“id”在通知有效载荷中作为JSON传递。

但我不知道如何处理

中的这些值
PushNotificationIOS.addEventListener('notification', this._onNotification.bind(this));

“通知”事件。

显然,在点按通知时会显示该应用,但是如果该功能一旦启动,该怎么办呢?

谢谢!

1 个答案:

答案 0 :(得分:1)

在您的index.ios.js中注册的最顶层组件中,在componentDidMount方法中,您可以从通知中添加您的监听器和get the data

export default class App extends Component {
    componentDidMount() {
        PushNotificationIOS.addEventListener('notification', this._onNotification.bind(this));
        if (notification) {
            const data = notification.getData();
            if (data) {
                // do your thing
            }
        }
    }
}

另一个解决方案是传递一个url,并在最顶层组件的componentDidMount方法中实现以下代码:

Linking.getInitialURL().then(url => {
  if (!url) {
    return null;
  }
  // do something to decide which component to load
  navigator.push({
    component: YourComponent,
    passProps: {
      // your props
    },
  });
  return null;
}).catch(() => null);