简体   繁体   English

设置 firebase 通知

[英]Setting up firebase notifications

So I've been working on setting up firebase notifications using cloud messaging and I've run into a problem.因此,我一直致力于使用云消息传递设置 firebase 通知,但遇到了问题。 I've been following 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 one.我也一直在看这个

Here's my AppDelegate code, which is the spot in which they told me to put all the stuff.这是我的 AppDelegate 代码,这是他们告诉我放置所有东西的地方。 I also did the certificate stuff, and hopefully, that works fine:我也做了证书的事情,希望它能正常工作:


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)
  }
}

The problem:问题:

Type 'Messaging' has no member 'messaging'

You might be using the old version of pods or dependencies.您可能正在使用旧版本的 pod 或依赖项。


FIRMessaging.messaging().delegate = self

Above code will work for you.上面的代码将为您工作。

Alternative选择

If you are using pod you can updating with the following command.如果您使用的是 pod,则可以使用以下命令进行更新。

pod update Firebase/Messaging

or just要不就

pod update

Above command will update your all pods, be careful.上面的命令将更新您的所有 pod,请小心。

Yay.好极了。 I found the solution.我找到了解决方案。 Pretty simple actually.其实很简单。 I accidentally had a swift view in my app named Messaging which was throwing off the syntax.我不小心在我的名为 Messaging 的应用程序中有一个 swift 视图,它丢弃了语法。

If you're having this problem.如果你遇到这个问题。 Make sure nothing in your project is named Messaging.确保项目中没有任何名称名为 Messaging。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM