[英]Flutter push notifications using FCM not working on iOS
幾周以來,我一直在嘗試讓推送通知在 iOS 上正常工作,但無濟於事。 我已經梳理了文檔以驗證我的配置。 然而,推送通知對於 Android 工作正常。
我還測試了直接從 firebase 消息控制台向 IOS 發送消息,但仍然不成功。 我還嘗試了之前堆棧溢出帖子中的許多建議,但均未成功。
https://github.com/FirebaseExtended/flutterfire/issues/1677
iOS FirebaseCloudMessaging 通知在調試/試飛和發布中不起作用
我在 iOS 14.6 上使用物理 iPhone 12。 我正在使用的 Xcode 的版本是 12.5。 Xcode 配置如下。
應用程序委托文件的代碼
import UIKit
import Flutter
import Firebase
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
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)
}
}
如何請求推送通知的代碼
Future<void> notficationsPermission () async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: true,
badge: true,
carPlay: false,
criticalAlert: true,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
String uid = Pref.getString(Keys.USER_ID);
var databaseReference = FirebaseDatabase.instance.reference();
if(settings.authorizationStatus == AuthorizationStatus.authorized){
notficationStatus = true;
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification
badge: true,
sound: true,
);
}
else{
notficationStatus = false;
}
}
}
如何配置通知的片段
return admin.messaging().sendToTopic(
topicName, {
android: {
priority: "high",
},
// Add APNS (Apple) config
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
"apns-push-type": "background",
"apns-priority": "5", // Must be `5` when `contentAvailable` is set to true.
"apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
},
},
notification: {
title: snapshot2.val().group_name +
": new chat message",
body: name +":"+snapshot.val().message,
clickAction: "FLUTTER_NOTIFICATION_CLICK",
},
});
我的 Info.plist 中也有以下內容。
<key>FirebaseAppDelegateProxyEnabled</key>
<string>0</string>
我終於想通了,但忘了發布答案。 在我的 index.js 中
exports.chatNotfi = functions.database.ref("messages/{gId}/{chat}")
.onCreate((snapshot, context)=>{
const groupId = context.params.gId;
console.log("Group id:" + groupId);
const topicName = groupId + "chat";
console.log("topic name"+topicName);
const userId = snapshot.val().userId;
return admin.database().ref("groups/"+groupId+ "/").once("value").
then((snapshot2)=>{
return admin.messaging().sendToTopic(
topicName, {
notification: {
title:
": New chat message",
body: name +":"+snapshot.val().message,
clickAction: "FLUTTER_NOTIFICATION_CLICK",
},
});
});
在我的 AppDelegate.swift
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)
}
}
在我的 Info.plist
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
還要確保在 firebase 控制台中注冊的應用程序與 Xcode 中使用的包標識符匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.