Реагировать на собственные данные push-уведомлений, показывая неопределенные - PullRequest
0 голосов
/ 16 апреля 2019

Я использую push-уведомление в своем собственном проекте реакции, и из push-уведомления я получаю идентификатор, но первые 2 раза он показывает мне «undefined», поэтому мой другой вызов навигатора и условие не выполнено.

Я создал новый стекавигатор для своего push-уведомления, но с идентификатором бронирования мое состояние каждый раз не удавалось.

componentWillUnmount() {
 this.createNotificationListeners();
}

Это моя функция push-уведомлений:

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));
      });
    }

А это мой код рендеринга

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

      }

  }

оно не будет неопределенным, и если я получу push-уведомление каждый раз, когда оно должно перейти в навигацию push-уведомлений.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...