You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS设备无法注册Firebase推送,同代码Android正常的问题求助

解决React Native Firebase iOS调用getToken()报错messaging/unregistered的问题

核心问题原因

iOS平台上,即便用户授权了通知权限,Firebase Messaging仍需要手动注册远程消息才能获取FCM Token,这是和Android平台的关键差异点,你的代码中缺失了这一必要步骤。

解决方案1:修改JS代码,补充远程消息注册

在权限授权通过后,先调用messaging().registerDeviceForRemoteMessages()完成注册,再执行Token获取操作。修改后的代码如下:

import messaging from '@react-native-firebase/messaging';

export const requestUserPermission = async () => {
    const authStatus = await messaging().requestPermission();
    const enabled =
        authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
        authStatus === messaging.AuthorizationStatus.PROVISIONAL;

    if (enabled) {
        console.log('Authorization status:', enabled);
        // iOS必须先执行远程消息注册
        try {
            await messaging().registerDeviceForRemoteMessages();
            getFcmToken();
        } catch (registerError) {
            console.log('注册远程消息失败:', registerError);
        }
    }
}

export const getFcmToken = async () => {
    try {
        const token = await messaging().getToken({ senderId: "727807064485" });
        console.log(token, ' TOKEN');
    } catch (error) {
        console.log("FCM error:", error)
    }
}

解决方案2:检查Xcode关键配置

  • 打开项目Capabilities标签,确保Push Notifications已开启
  • 勾选Background Modes下的Remote notifications选项
  • 确认GoogleService-Info.plist已正确添加到项目,且文件的Target Membership已勾选对应项目

解决方案3:验证AppDelegate配置

确保AppDelegate.mm中已正确添加Firebase Messaging代理逻辑,关键代码片段如下:

#import <Firebase.h>
#import <React/RCTAppDelegate.h>
#import <UserNotifications/UserNotifications.h>
#import <RNFBApp/RNFBAppModule.h>
#import <RNFBMessaging/RNFBMessagingModule.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  UNUserNotificationCenter.currentNotificationCenter.delegate = self;
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// 前台通知展示处理
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

// APNs Token注册成功回调
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNFBMessagingModule didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// APNs Token注册失败回调
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RNFBMessagingModule didFailToRegisterForRemoteNotificationsWithError:error];
}

@end

额外验证步骤

  • 确认iOS设备的应用通知权限已真正开启(设置→对应应用→通知)
  • 执行pod install后,通过Xcode的Product→Clean Build Folder清理缓存,再重新编译运行

内容的提问来源于stack exchange,提问作者Rahul Jalandhra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:40:22