【问题标题】:how to navigate to specific screen when press the notification in react native在反应原生中按下通知时如何导航到特定屏幕
【发布时间】:2020-11-02 10:20:41
【问题描述】:

我将 react native push notificationfirebase 一起使用。我收到通知,一切正常,但问题是 OnOpenNotfication 函数启动了两次。它第一次显示 {} 并自动调用自身。当我按下通知时,它也会触发并且什么都没有显示,我的意思是通知什么都没有。 但是我只想在单击通知时调用 onOpenNotification。而且我还想将用户从 onOpenNotfication 导航到特定屏幕,但它会自动启动,这就是为什么我不能 这是代码。

 componentDidMount = async () => {
   
    fcmService.registerAppWithFCM();
    fcmService.register(onRegister, onNotification, onOpenNotification);
    localNotificationService.configure(onOpenNotification);

    function onRegister(token) {
      console.log('[App] onRegister: ', token);
    }

    function onNotification(notify) {
      console.log('[App] onNotification: ', notify);
      const options = {
        soundName: 'default',
        playSound: true, //,
        // largeIcon: 'ic_launcher', // add icon large for Android (Link: app/src/main/mipmap)
        // smallIcon: 'ic_launcher' // add icon small for Android (Link: app/src/main/mipmap)
      };
      localNotificationService.showNotification(
        0,
        notify.title,
        notify.body,
        notify,
        options,
      );
    }

    function onOpenNotification(notify) {
      console.log('[App] onOpenNotification: ', notify);
      alert('Open Notification: ' + notify.body);
    }
  };

【问题讨论】:

    标签: react-native react-native-firebase react-native-push-notification


    【解决方案1】:

    希望对你有所帮助。你可以试试这个逻辑。

     import firebase from 'react-native-firebase';
        
        componentDidMount(){
            firebase
                .notifications()
                .onNotificationOpened((notificationOpen: NotificationOpen) => {
                  console.log('onNotificationOpened', notificationOpen);
                  this.handleNotification(notificationOpen.notification, false);
                });
        }
        
        handleNotification(notification: Notification, appClosed){
           //notification will have your custom data and here you can apply your logics
           //you can use navigation here according to your logic.
           navigatorRef.navigate("routeName");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多