【问题标题】:React Native handle IOS permissions for FCM messagingReact Native 处理 FCM 消息传递的 IOS 权限
【发布时间】:2020-06-05 17:03:41
【问题描述】:

我将 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 ? 

【问题讨论】:

    标签: android ios firebase react-native firebase-cloud-messaging


    【解决方案1】:

    当您请求许可时,如果用户拒绝或取消请求,您可以询问用户应用需要该许可并将他重定向到应用设置页面,,, 您可以检查每次启动应用程序时是否授予天气权限,如果没有将它们重定向到设置页面,因为如果用户拒绝它,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();
    

    【讨论】:

    • 我如何实现函数 setNotification 和 onNotification ?我正在使用 import firebase from '@react-native-firebase/app' import messages from '@react-native-firebase/messaging' 我找不到这两个函数
    • 目前在 react native firebase v6 中没有通知,所以如果你需要在你的应用中通知你需要降级到 v5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2021-01-19
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多