React native push notification data showing undefined

亡梦爱人 提交于 2019-12-22 01:05:46

问题


I am using push notification in my react native project and from the push notification i am getting id but first 2 times it's showing me "undefined" so my another navigator call and condition failed.

I created new stacknavigator for my push notification but with booking id my condition every time failed.

componentWillUnmount() {
 this.createNotificationListeners();
}

This is my push notification function:

async createNotificationListeners() {

      const channel = new firebase.notifications.Android.Channel(
        'appname',
        'appname',
        firebase.notifications.Android.Importance.Max
      ).setDescription('my beauty stylist');
      firebase.notifications().android.createChannel(channel);

      /*
      * Triggered when a particular notification has been received in foreground
      * */
      this.notificationListener = firebase.notifications().onNotification((notification) => {
          const { title, body, id } = notification.data;
          console.log("Notification Title: "+title);
          console.log("Notification body: "+body);
          console.log("Notification id: "+id);
          this.setState({ notificationscreen: true })
          this.bookingId = id;

          const localNotification = new firebase.notifications.Notification({
            sound: 'default',
            show_in_foreground: true,
          })
          .setNotificationId(notification.notificationId)
          .setTitle(notification.title)
          .setBody(notification.body)
          .setData(notification.data)
          .android.setAutoCancel(true)
          .android.setChannelId('mybeautysquad') // e.g. the id you chose above
          .android.setSmallIcon('ic_launcher') // create this icon in Android Studio
          .android.setColor('#000000') // you can set a color here
          .android.setPriority(firebase.notifications.Android.Priority.High);

          firebase.notifications()
          .displayNotification(localNotification)
          .catch(err => console.error(err));
      });

      /*
      * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
      * */
      this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
          const { title, body, id } = notificationOpen.notification.data;
          console.log("Pubg",notificationOpen.notification.data);
          this.setState({ notificationscreen: true })
          this.bookingId = id;
          // this.showAlert(title, body, id);
      });

      /*
      * 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) {
          const { title, body, id } = notificationOpen.notification.data;
          this.setState({ notificationscreen: true })
          this.bookingId = id;
          // this.showAlert(title, body, id);
      }
      /*
      * Triggered for data only payload in foreground
      * */
      this.messageListener = firebase.messaging().onMessage((message) => {
        //process data message
        console.log(JSON.stringify(message));
      });
    }

And this is my render code

render() {
    console.log("app js "+this.bookingId);
(Output: app js undefined)
    console.log("notificationscreen: "+notificationscreen);
(Output: app js undefined)
    const { checkedSignIn, signedIn, notificationscreen } = this.state;
      if (!checkedSignIn) {
        return null;
      }
      if(notificationscreen && this.bookingId != "undefined"){
        return <AppStackNavigatorNew screenProps={{ bookingId: this.bookingId }} />;
      }else{
        return <AppStackNavigator1  screenProps={{ bookingId: this.bookingId }} />;

      }

  }

it won't undefined and if i got push notification every time it should move to push notification navigation.

来源:https://stackoverflow.com/questions/55710653/react-native-push-notification-data-showing-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!