ios - Firebase 推送通知不适用于 iOS

标签 ios swift firebase firebase-cloud-messaging apple-push-notifications

我想使用 Firebase Cloud Messaging 实现推送通知

我已经按照说明设置了我的项目并上传了 APN 证书 我正在使用 fcmtoken 向我的真实设备发送测试消息

我在AppDelegate中的配置如下

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        registerForPushNotifications(app: application)
        return true
    }

    func registerForPushNotifications(app: UIApplication) {
        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (authorized, error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            if authorized {
                print("authorized")
                DispatchQueue.main.async {
                  UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                print("denied")
            }
        }
        app.registerForRemoteNotifications()
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        let dataDict:[String: String] = ["token": fcmToken]
         NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
         // TODO: If necessary send token to application server.
         // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    func application(_ application: UIApplication,
        didReceiveRemoteNotification notification: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("notofication arrivied")
      if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
      }
      // This notification is not auth related, developer should handle it.
    }

它应该看到通知已到达,但它没有还设置了一个喙点似乎这部分永远不会被原谅,因此消息不会到来

最佳答案

除非您启用了 Swizzling,否则我不会在您的 AppDelegate 中看到此内容

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
      Messaging.messaging().apnsToken = deviceToken
    }

此代码将您的 APNs 设备 token 映射到 FCM token ,这是必要的,因为 APNs token 是您发送推送通知的唯一方式。

关于ios - Firebase 推送通知不适用于 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096175/

相关文章:

ios - Objective-C 中 UIScrollView 内具有动态高度的 UIWebView

ios - PageviewController 选择索引到特定的 viewcontroller

Swift 3.0 向下转换和最佳实践

javascript - 简单的 Firestore 查询无法为我提供集合中的完整文档

swift - Firebase 数据与 Eureka 和 Init

androidx.room.RoomOpenHelper.e (RoomOpenHelper.java :15)

iphone - 具有不同属性的 NSFetchRequest

iphone - 如何在 iOS 应用程序上只运行一次代码(甚至应用程序将被删除并重新安装)?

swift - 快速使用 Metal 拍摄当前屏幕的快照

swift - 在 Swift 的元组中是否可以有一个 nil 值?