【发布时间】: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