RNFirebase v6推送通知都不同时在iOS和Android上出现

问题描述 投票:0回答:1

我正在尝试将通知从Firebase控制台发送到我的react-native应用

据我所知,我在这里遵循了糟糕的文档:https://invertase.io/oss/react-native-firebase/v6/messaging/quick-start

我安装了@ react-native-firebase / app和/ messaging,这是组件中的代码:

  componentDidMount() {

    this.reqNotifications()
    this.checkNotificationPermission()

  }


  reqNotifications() {

    requestNotifications(['alert', 'badge', 'sound']).then(({status, settings}) => {

      console.log('NOTIFICATION STATUS' + status)

    });

  }

  async checkNotificationPermission() {
    const enabled =  await messaging().hasPermission();
    if (enabled) {
      console.log('APPROVED');
      await messaging().registerForRemoteNotifications()
      messaging().getToken().then(token => console.log('token: >> ' + token))



    } else {
      console.log('NOT APPROVED');
    }
  }
  • 我正在通过react-native-permissions请求许可,并且许可请求为工作
  • 我的Apple APN在Apple和Firebase控制台上为确定
  • 而且我正在通过代码上的getToken()方法获取令牌。成功

但是我无法从firebase向设备发送任何东西;前景和背景都没有发生。我尝试了令牌测试,也尝试了正常,但没有,什么也没有发生。

我将此代码添加到componentDidMount:

messaging().onMessage(async remoteMessage => {
  console.log('FCM Message Data:', remoteMessage.data);
});

据我所知,这订阅了云消息,当我从firebase-console发送一些云消息通知时,我应该获得控制台输出; 但什么也没发生。

我不知道我缺少什么,但是我认为这个软件包有很大的更新,并且大多数文档都适用于以前的版本,我真的被困在这里,感谢您的帮助

firebase react-native google-cloud-messaging react-native-firebase react-native-push-notification
1个回答
0
投票
 componentDidMount = async () => {
    this.checkNotificationPermission();
    await messaging().requestPermission({provisional: true});
    await messaging().registerDeviceForRemoteMessages();

    await this.getFCMToken();
    if (Platform.OS === 'android') {
      this.createAndroidNotificationChannel();
    }

    this.backgroundState();
    this.foregroundState();
  };   
checkNotificationPermission = () => {
    firebase
      .messaging()
      .hasPermission()
      .then(enabled => {
        if (!enabled) {
          this.promptForNotificationPermission();
        }
      });
  };

  promptForNotificationPermission = () => {
    firebase
      .messaging()
      .requestPermission({provisional: true})
      .then(() => {
        console.log('Permission granted.');
      })
      .catch(() => {
        console.log('Permission rejected.');
      });
  };

  createAndroidNotificationChannel() {
    const channel = new firebase.notifications.Android.Channel(
      'channelId',
      'Push Notification',
      firebase.notifications.Android.Importance.Max,
    ).setDescription('Turn on to receive push notification');

    firebase.notifications().android.createChannel(channel);
  }
 foregroundState = () => {
    const unsubscribe = messaging().onMessage(async notification => {
      console.log('Message handled in the foregroundState!', notification);
    });

    return unsubscribe;
  };

  // Register background handler
  backgroundState = () => {
    messaging().setBackgroundMessageHandler(async notification => {
      console.log('Message handled in the background!', notification);
    });
  };
© www.soinside.com 2019 - 2024. All rights reserved.