我正在构建一个适用于 5 个平台的应用程序 - android、iOS、macOS、Windows 和 Web。 现在,我需要为以上 5 个平台实现推送通知。 我正在使用 firebase_messaging 插件进行推送通知。它适用于 android 和 iOS,但不适用于 macOS。 尝试获取 fcm 令牌时抛出错误
[firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling getAPNSToken()
。
我已经按照this设置了firebase。 即使我已经为所有支持的平台仔细初始化了插件。
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()));
}
默认FirebaseOptions类:
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: "....."
);
}
通知服务类:
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;
}
}
}
在 Firebase 上:
转到设置 > 项目设置 > 常规
向下滚动找到 iOS 应用程序
添加团队 ID 和 App Store ID
转到“云消息传递”选项卡并确保您已上传身份验证密钥(p8 文件)。相同的内容是从开发者控制台 > 密钥生成的。
此外,请确保您还添加了团队 ID 和 App Store ID。
在 Xcode 上:
我在 iOS 上遇到了同样的问题,身份验证密钥(p8)文件丢失,团队 ID 和 App Store Id 也丢失,添加这解决了我的问题。