React Native 0.69版本Firebase推送令牌获取失败问题求助
解决React Native集成Firebase推送令牌获取失败问题
问题描述
React Native 0.69版本,集成@react-native-firebase/app与@react-native-firebase/messaging实现推送通知,本地通知发送正常,但调用getToken()获取FCM令牌时出错。相关代码如下:
import PushNotificationIOS from "@react-native-community/push-notification-ios"; import PushNotification from "react-native-push-notification"; import firebase from "@react-native-firebase/app"; import messaging from "@react-native-firebase/messaging"; export default class App extends React.Component { componentDidMount() { firebase.messaging().registerDeviceForRemoteMessages() .then((res)=>console.log(res)) .catch((e)=>console.log(e)); firebase.messaging().hasPermission() .then((enabled) => { firebase.messaging().getToken().then(token => console.log(token)); }) } render() { return <View></View> } } PushNotification.configure({ onRegister: function (token) { console.log("TOKEN:", token); }, onNotification: function (notification) { console.log("NOTIFICATION:", notification); notification.finish(PushNotificationIOS.FetchResult.NoData); }, onAction: function (notification) { console.log("ACTION:", notification.action); console.log("NOTIFICATION:", notification); }, onRegistrationError: function(err) { console.error(err.message, err); }, permissions: { alert: true, badge: true, sound: true, }, popInitialNotification: true, requestPermissions: true, });
错误提示:令牌获取请求因权限或配置问题被拒绝
解决方案
1. 统一权限请求逻辑,避免依赖冲突
你同时使用了react-native-push-notification和Firebase Messaging,两者的权限请求逻辑易产生冲突。建议统一用Firebase Messaging的API处理权限:
async componentDidMount() { try { // 请求推送权限 const authStatus = await messaging().requestPermission(); const hasPermission = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (hasPermission) { // 注册设备并获取令牌 await messaging().registerDeviceForRemoteMessages(); const token = await messaging().getToken(); console.log('FCM令牌:', token); } else { console.log('推送权限被用户拒绝'); } } catch (error) { console.error('令牌获取失败详情:', error.code, error.message); } }
同时修改PushNotification.configure配置,关闭重复权限请求:
PushNotification.configure({ // ...其他配置 requestPermissions: false, // 禁止重复请求权限 });
2. 检查iOS平台配置
- 打开Xcode,在项目的Signing & Capabilities中添加
Push Notifications能力; - 开启
Background Modes,勾选Remote notifications; - 确认
GoogleService-Info.plist已通过Xcode添加到项目中(直接拖入文件夹可能不生效)。
3. 检查Android平台配置
- 将
google-services.json放在android/app目录下; - 在
AndroidManifest.xml中添加必要权限:<uses-permission android:name="android.permission.INTERNET" /> <!-- Android 13及以上需要添加该权限 --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> - 确认
build.gradle配置正确:
在android/build.gradle的dependencies中添加:
在classpath 'com.google.gms:google-services:4.3.15'android/app/build.gradle末尾添加:apply plugin: 'com.google.gms.google-services'
4. 监听令牌刷新事件
添加令牌刷新监听,确保令牌更新时能及时获取:
componentDidMount() { // ...其他逻辑 messaging().onTokenRefresh(newToken => { console.log('FCM令牌已刷新:', newToken); // 这里可以将新令牌同步到服务器 }); }
内容的提问来源于stack exchange,提问作者user
相关产品推荐
相关产品推荐

