React Native iOS应用无法获取APNs Token导致FCM令牌无法生成的问题求助
React Native iOS应用无法获取APNs Token导致FCM令牌无法生成的问题求助
各位开发者前辈好,我在React Native iOS项目(项目名LesFemmes)里遇到了推送功能的棘手问题:没法获取APNs Token,直接导致FCM令牌也生成不了,Firebase推送在iOS设备上完全跑不起来。我已经配置了Firebase的.p8文件,但问题依然存在。下面是我的AppDelegate.m完整代码,麻烦帮忙看看哪里出了问题,给点修复思路,万分感谢!
#import <Firebase.h> #import <RNCPushNotificationIOS.h> #import <React/RCTBundleURLProvider.h> #import "RNSplashScreen.h" #import <UserNotifications/UserNotifications.h> #import <FirebaseMessaging/FirebaseMessaging.h> @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.moduleName = @"LesFemmes"; self.initialProps = @{}; [RNSplashScreen show]; // Firebase setup [FIRApp configure]; [FIRMessaging messaging].delegate = self; // Set UNUserNotificationCenter delegate [UNUserNotificationCenter currentNotificationCenter].delegate = self; // Request user permission for notifications UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { if (error) { NSLog(@"Error in APNs authorization: %@", error); } if (granted) { NSLog(@"✅ Notification permission granted"); } else { NSLog(@"❌ Notification permission denied"); } }]; // ✅ Always register for remote notifications — even if permission was already granted dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); return [super application:application didFinishLaunchingWithOptions:launchOptions]; } // Called when the device successfully registers with APNs - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"✅ didRegisterForRemoteNotificationsWithDeviceToken called"); NSLog(@"APNs Device Token: %@", deviceToken); [FIRMessaging messaging].APNSToken = deviceToken; const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes]; NSMutableString *hexToken = [NSMutableString string]; for (NSInteger i = 0; i < deviceToken.length; i++) { [hexToken appendFormat:@"%02x", tokenBytes[i]]; } NSLog(@"🔑 APNs Token Hex: %@", hexToken); } // Called when Firebase generates a new FCM token - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken { NSLog(@"🔥 Firebase Registration Token (FCM): %@", fcmToken); // TODO: Send the FCM token to your backend server here } // Required to support foreground push notifications (iOS 10+) - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge); } // Required to handle notification tap actions (when app is in background or killed) - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { completionHandler(); } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { return [self bundleURL]; } - (NSURL *)bundleURL { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } @end
我整理的排查&修复方向:
- 必须用真实iOS设备测试:模拟器完全不支持APNs推送,这是新手最容易踩的坑,一定要拿实体设备测!
- 补全APNs注册失败的回调方法:你现在的代码缺少关键的错误反馈入口,苹果会在注册失败时返回具体错误信息,赶紧加上这个方法,能帮你精准定位问题:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"❌ APNs注册失败:%@", error.localizedDescription); NSLog(@"错误详情:%@", error); } - 检查Xcode项目核心配置:
- 进入项目「Signing & Capabilities」,确认已经添加「Push Notifications」能力,且对应的Provisioning Profile包含推送权限(可去苹果开发者后台验证Profile权限)
- 确保项目Bundle ID和Firebase控制台注册的iOS应用Bundle ID完全一致(大小写、特殊字符都不能错)
- 确认
GoogleService-Info.plist已正确添加到项目,且Xcode右侧「Target Membership」勾选了你的项目目标
- 验证Firebase推送配置:
- 打开Firebase控制台对应iOS应用的「Cloud Messaging」页面,检查「APNs Authentication Key」:.p8文件、Team ID、Key ID必须和苹果开发者后台生成的完全匹配
- 开发/生产环境的推送配置是分开的,测试时要确保对应环境的配置正确
- 权限&网络检查:
- 打开iOS设备「设置-通知」,确认你的应用已开启所有通知权限
- 设备需要能正常连接苹果APNs服务器,测试时尽量用稳定网络(避免VPN或受限网络)
如果有新的错误日志或排查进展,欢迎补充信息!
内容来源于stack exchange
相关产品推荐
相关产品推荐

