React Native 处理 FCM 消息传递的 IOS 权限

React Native handle IOS permissions for FCM messaging

我将 FCM 消息传递集成到我的 React Native 应用程序中。

在我的 App 组件中,我试图通过询问用户是否接受接收通知来授予权限。

我的问题是,我授予权限的方式是否正确?

我使用 async componentDidMount 来实现函数 requestPermission :

import firebase from '@react-native-firebase/app'
import messaging from '@react-native-firebase/messaging'

async componentDidMount () {
 // ......
    const granted = messaging().requestPermission()
    if (granted) {
  console.log('User granted messaging permissions!')
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
  firebase
    .messaging()
    .getToken()
    .then(fcmToken => {
      if (fcmToken) {
        // user has a device token
        console.log('fcm')
        console.log(fcmToken)
      } else {
        // user doesn't have a device token yet
        console.log('error')
      }
    })
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
} else {
  console.log('User declined messaging permissions :(')
}
}

I get the FCM generated token, and i can send message using Postman to the device using this token. How can i be sure that the permissions are always granted and i can get the token from every device ? 

当您请求许可时,如果用户拒绝或取消请求,您可以询问用户该应用程序需要该许可并将他重定向到应用程序设置页面, 您可以在每次启动应用程序时检查是否授予天气权限,如果没有将它们重定向到设置页面,因为 ios 仅在用户拒绝时询问权限一次,不会再次询问权限。这是一个示例代码用于检查并请求权限

 firebase
      .messaging()
      .hasPermission()
      .then(enabled => {       
        if (enabled) {
          setNotification();
          onNotification();
        } else {
          firebase
            .messaging()
            .requestPermission()
            .then()
            .catch(err => {
              if (!isAndroid) {
                Linking.canOpenURL('app-settings:')
                  .then(supported => {
                    Linking.openURL('app-settings:');
                  })
                  .catch(error => {});
              }
            });
       }
     })
.catch();