【发布时间】:2021-02-11 10:07:23
【问题描述】:
我已经实施了 FCM。没有错误显示。令牌成功生成。但没有通知进入设备。
我遵循了谷歌文档。
这是我的 AppDelegate 类
import Foundation
import UIKit
import Firebase
//https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print(userInfo)
}
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(String(describing: fcmToken))")
let dataDict:[String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
}
didReceiveRegistrationToken 委托方法调用两次
我尝试从 firebase 云消息传递选项发送通知,但没有发送到任何 Push in 设备。 我也试过邮递员。这是我的有效载荷和输出
我尝试了主项目,但它不工作。 然后我尝试重新实施到一个虚拟项目中,它也不起作用。
这是我的源代码。你可以检查一下这个。 https://www.dropbox.com/s/22wojqv5u3vwjzt/Swift%20UI%20Push%20Notification.zip?dl=0
【问题讨论】:
-
检查有效负载,但在文档中 APNS 的 fcm 有效负载不同
-
在 iOS 上使用 FCM 总是很困难,但在 Android 上总是运行得更好。曾经与 Firebase 团队就他们的 iOS 不稳定 FCM 进行了长时间的讨论。以后请使用 APNs,它总是很稳定的。
标签: ios swift swiftui firebase-cloud-messaging apple-push-notifications