所以我一直在使用云消息设置firebase通知,但遇到了一个问题。我一直在学习这个教程,所以如果我做错了什么,希望我没有做错,请告诉我。我也一直在看这个。
这是我的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'
您可能正在使用旧版本的pod或依赖项。
FIRMessaging.messaging().delegate = self
以上代码将适用于您。
替代
如果您正在使用pod,您可以使用以下命令进行更新。
pod update Firebase/Messaging
或者只是
pod update
以上命令将更新您的所有pod,请小心。
是的。我找到了解决办法。其实很简单。我无意中在我的名为Messaging的应用程序中看到了一个快速视图,该应用程序抛出了语法。
如果你有这个问题。请确保项目中没有命名为Messaging的内容。