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

Flutter macOS端集成Firebase Messaging遇APNS Token未设置错误

macOS端Flutter Firebase Messaging获取FCM Token失败问题

我正在开发一款支持Android、iOS、macOS、Windows及Web五端的Flutter应用,当前需为这五个平台实现推送通知功能。使用firebase_messaging插件时,Android和iOS端运行正常,但macOS端无法正常工作,尝试获取FCM Token时抛出错误:
[firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling getAPNSToken()

我已按照Firebase官方Apple集成文档完成配置,且已针对支持平台仔细初始化插件,相关代码如下:

main函数

void main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  if (kIsWeb ||
      (Platform.isAndroid ||
          Platform.isIOS ||
          Platform.isMacOS 
          )) {
    NotificationService.init(); // calling it here to get fcmToken for testing
  } 
 NotificationService.getFcmToken();

runApp(MultiBlocProvider(providers: [
    BlocProvider<NetworkBloc>(
        create: (_) => NetworkBloc()..add(ListenConnection())),
    BlocProvider(create: (context) => SocketCubit()),
    BlocProvider(
      create: (context) => HomeCubit(),
    ),
    BlocProvider<ProfileCubit>(create: (_) => ProfileCubit()),
  ], 
  child: const MyApp()));
}

DefaultFirebaseOptions类

class DefaultFirebaseOptions {
  static FirebaseOptions get currentPlatform {
    if (kIsWeb) {
      return web;
    }
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return android;
    case TargetPlatform.iOS:
      return ios;
    case TargetPlatform.macOS:
      return macos;
      case TargetPlatform.windows:
      //  return windows;
      throw UnsupportedError(
          'DefaultFirebaseOptions have not been configured for windows - '
              'you can reconfigure this by running the FlutterFire CLI again.',
        );
      case TargetPlatform.linux:
        throw UnsupportedError(
          'DefaultFirebaseOptions have not been configured for linux - '
              'you can reconfigure this by running the FlutterFire CLI again.',
        );
      default:
        throw UnsupportedError(
          'DefaultFirebaseOptions are not supported for this platform.',
        );
    }
  }

  static const FirebaseOptions web = FirebaseOptions(
      apiKey: "AIzaSy....",
      authDomain: "...",
      projectId: "...",
      storageBucket: "...",
      messagingSenderId: "......",
      appId: "...",
      measurementId: "..."
  );

  static const FirebaseOptions android = FirebaseOptions(
    apiKey: 'AIz...',
    appId: '....',
    messagingSenderId: '....',
    projectId: '....',
    storageBucket: '....',
  );

static const FirebaseOptions ios = FirebaseOptions(
  apiKey: 'AIz....',
  appId: '....',
  messagingSenderId: '...',
  projectId: '...',
  storageBucket: '...',
);

static const FirebaseOptions macos = FirebaseOptions(
    apiKey: "AIz...",
    authDomain: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "....",
    appId: "...",
    measurementId: "....."
);
}

NotificationService类

class NotificationService {
 static FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
 static init() async {
    await firebaseMessaging.requestPermission();
    
  }

static Future<String?> getFcmToken() async {
    try {
      String? token;
      if (kIsWeb || Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
        token = await firebaseMessaging.getToken();
      }
      // else if( Platform.isMacOS){
      //   await Future.delayed(const Duration(seconds: 3));
      //     token = await firebaseMessaging.getAPNSToken();

      // }
      else if (Platform.isWindows) {
        token = '';
      }

      debugPrint("FCM TOKEN------------->$token");
      return token;
    } catch (e) {
      debugPrint("FCM TOken error ----------->$e");
      rethrow;
    }
  }
}

macOS FCM Token获取错误截图

内容的提问来源于stack exchange,提问作者Shruti Ramnandan Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 03:02:16