如何退订react-native-firebase getInitialNotification

时间:2018-11-28 09:20:45

标签: javascript reactjs firebase react-native react-native-firebase

我在react-native-firebase应用程序中使用react-native进行推送通知。

收到通知后,通过单击通知打开应用程序(这可以通过触发getInitialNotification函数来正常运行)。但是,当我从一个屏幕导航到另一个屏幕时,它将再次被触发。如何停止呢?触发此事件后,还有其他方法可以取消订阅吗? 还是对此问题有任何解决方法?

class Header extends Component {
  async componentDidMount() {
    this.checkPermission();
    this.createNotificationListeners();
  }

  componentWillUnmount() {
    this.notificationListener();
    this.notificationOpenedListener();
  }

  async checkPermission() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
        this.getToken();
    }
  }

  async getToken() {
   //token retrieve code
  }

  async createNotificationListeners() {

    firebase.messaging().subscribeToTopic("all")

    /*
    * If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
    * */
    const notificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
        alert(title, body);

    }
  }

  render() {
    return (
        <View></View>
    );
  }
}

0 个答案:

没有答案