设置 Firebase 通知

Setting up firebase notifications

所以我一直致力于使用云消息传递设置 firebase 通知,但我 运行 遇到了问题。我一直在关注 this tutorial, so if I did anything else wrong, which hopefully I didn't, please let me know. I've also been looking at this 一个。

这是我的 AppDelegate 代码,这是他们告诉我放置所有东西的地方。我也做了证书的事情,希望它能正常工作:


import UIKit
import Firebase
import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions
      launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
      
      FirebaseApp.configure()
      // 2
      FirebaseConfiguration.shared.setLoggerLevel(.min)
      
      UNUserNotificationCenter.current().delegate = self
      // 2
      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
      UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions) { _, _ in }
      // 3
      application.registerForRemoteNotifications()
      
      Messaging.messaging().delegate = self //Here is one of the problems
    return true
  }
}
    
    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler:
    @escaping (UNNotificationPresentationOptions) -> Void
  ) {
    completionHandler([[.banner, .sound]])
  }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {
    completionHandler()
  }
}

func application(
  _ application: UIApplication,
  didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
  Messaging.messaging().apnsToken = deviceToken //Here's the other problem
}

extension AppDelegate: MessagingDelegate {
  func messaging(
    _ messaging: Messaging,
    didReceiveRegistrationToken fcmToken: String?
  ) {
    let tokenDict = ["token": fcmToken ?? ""]
    NotificationCenter.default.post(
      name: Notification.Name("FCMToken"),
      object: nil,
      userInfo: tokenDict)
  }
}

问题:

Type 'Messaging' has no member 'messaging'

您可能正在使用旧版本的 pods 或依赖项。


FIRMessaging.messaging().delegate = self

以上代码适合您。

备选

如果您使用的是 pod,则可以使用以下命令进行更新。

pod update Firebase/Messaging

或者只是

pod update

以上命令会更新你的所有pods,小心。

是的。我找到了解决方案。其实很简单。我不小心在名为 Messaging 的应用程序中有一个 swift 视图,该视图不符合语法。

如果您遇到这个问题。确保您的项目中没有任何内容名为 Messaging。