Firebase消息非常适用于Android,但iOS版本无法接收通知。我遵循本教程向应用程序添加推送通知:https://www.youtube.com/watch?v=u-7ut phOrA。唯一的区别是,我在苹果开发者门户网站上已经有了一个应用程序标识符。因此,我没有创建新的标识符,而是在现有标识符上启用推送通知。
在应用程序中要求权限如下:
void requestAndRegisterNotification() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print(
'Message title: ${message.notification?.title}, body: ${message.notification?.body}, data: ${message.data}');
});
} else {
print('User declined or has not accepted permission');
}
}
该函数是在InitState中调用的,我可以看到应用程序在测试时正在请求权限。
开发人员门户中的证书如下所示:证书。标识符如下所示:标识符。推送通知的Key如下所示:Key。Firebase Cloud Messasing APN看起来像这样:Firebase。
Runner中的AppDelegate文件已修改为:
import UIKit
import Flutter
import Firebase
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}
我不知道问题是从哪里来的,因为任何地方都没有明确的错误信息。Android版本如预期的那样运行,但iOS甚至没有收到物理设备的推送通知。即使我试图直接从Firebase向特定的FCM令牌发送通知。
根据Firebase文档启用后台提取和远程通知后台执行模式