简体   繁体   中英

Type 'Notification' has no member 'Name' - Swift

I'm trying to incorporate push notifications into my project, following the firebase documentation. However, I'm getting an error that I can't get around. "Type 'Notification' has no member 'Name'". Thanks in advance!

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        
        registerForPushNotifications(application)
        Messaging.messaging().delegate = self
        Messaging.messaging().subscribe(toTopic: "relationship") { (error) in
            print("subscribed to relationship topic..")
        }
        return true
    }
    
    func registerForPushNotifications(_ application: UIApplication){
        if #available(iOS 10.0, *) {
          // 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 })
        } else {
          let settings: UIUserNotificationSettings =
          UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
          application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()
    }
        
    // 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 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)
      // 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, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("DEBUG: registered for notifications with device token: \(deviceToken)")
    }
   
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.banner)
    }

}

The pods I have installed in my project:

  • pod 'Firebase/Core'
  • pod 'Firebase/Functions'
  • pod 'Firebase/Analytics'
  • pod 'Firebase/Auth'
  • pod 'Firebase/Firestore'
  • pod 'Firebase/Storage'
  • pod 'Firebase/Messaging'

Screenshot of my code

Your own "Notification" might be conflicting with Foundation's "Notification. If you put "NS" before notifications, should solve this problem.

NSNotification.Name

Check to see if you also have Notification defined in your project elsewhere, I had the same issue and turns out I already had defined "Notification" elsewhere. Adding "NS" to my new definition helped separate out the issue. like this -> extension NSNotification.Name { static let tapBackButton = NSNotification.Name("randomName") }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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