Android 响应FCM消息的本机句柄IOS权限
我将FCM消息集成到我的React本机应用程序中 在我的应用程序组件中,我试图通过询问用户是否接受接收通知来授予权限 我的问题是,我是否使用正确的方式授予权限 我使用async componentDidMount实现函数requestPermission:Android 响应FCM消息的本机句柄IOS权限,android,ios,firebase,react-native,firebase-cloud-messaging,Android,Ios,Firebase,React Native,Firebase Cloud Messaging,我将FCM消息集成到我的React本机应用程序中 在我的应用程序组件中,我试图通过询问用户是否接受接收通知来授予权限 我的问题是,我是否使用正确的方式授予权限 我使用async componentDidMount实现函数requestPermission: import firebase from '@react-native-firebase/app' import messaging from '@react-native-firebase/messaging' async compone
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();
如何实现setNotification和onNotification功能?我正在使用从“@react native firebase/app”导入firebase从“@react native firebase/messaging”导入消息,我找不到这2个功能。当前在react native firebase的v6中找不到此功能,因此如果您的应用中需要通知,则需要降级到v5