使用未声明的类型“FIRMessagingDelegate”、“Messaging”和“FIRMessagingRemoteMessage”。

Posted

技术标签:

【中文标题】使用未声明的类型“FIRMessagingDelegate”、“Messaging”和“FIRMessagingRemoteMessage”。【英文标题】:Use of undeclared type 'FIRMessagingDelegate', 'Messaging' and 'FIRMessagingRemoteMessage'. 【发布时间】:2017-08-11 18:44:51 【问题描述】:

我搜索了很长时间,但没有任何答案能解决我的问题。 我正在关注 Firebase 推送通知教程并实现了 AppDelegate 代码。

但 Xcode 无法识别“FIRMessagingDelegate”、“Messaging”和“FIRMessagingRemoteMessage”。

我尝试过更新 repo、重新创建等。

代码如下:

import UIKit
import CoreData
import Firebase
import UserNotifications
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    // Override point for customization after application launch.
    //FirebaseApp.configure() - Documentação 
    FIRApp.configure()

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

    return true


//More code..



// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate 

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
        let userInfo = notification.request.content.userInfo


        // Print full message.
        print(userInfo)

        // Change this to your preferred presentation option
        completionHandler([])
    

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) 
        let userInfo = response.notification.request.content.userInfo


        // Print full message.
        print(userInfo)

        completionHandler()
    



extension AppDelegate : FIRMessagingDelegate 
    // [START refresh_token]
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) 
        print("Firebase registration token: \(fcmToken)")
    
// [END refresh_token]
// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: FIRMessagingRemoteMessage) 
        print("Received data message: \(remoteMessage.appData)")
    
    // [END ios_10_data_message]
 //*/

【问题讨论】:

【参考方案1】:

您使用的是哪个版本的 Firebase?根据documentation,Swift 中 Firebase 4.0.0 的类名发生了变化。所以FIRMessagingDelegate,现在是MessagingDelegate,以此类推。请参阅迁移指南here

【讨论】:

【参考方案2】:

检查您是否拥有最新版本的 firebase 推送通知库。 他们的一些旧东西正在被弃用和删除。 如果这些 pod 已安装在您的项目中,请查看项目 Navigator/Pods.xcodeproj/Targets,如果没有将它们添加到您的 Podfile

【讨论】:

【参考方案3】:

FIRMessagingRemoteMessage 更新为MessagingRemoteMessage

【讨论】:

以上是关于使用未声明的类型“FIRMessagingDelegate”、“Messaging”和“FIRMessagingRemoteMessage”。的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire,使用未声明的类型

使用未声明的类型“CostumTableViewCell”

使用未声明的类型“UIViewController”

Objective-C 库的 Swift “使用未声明的类型”

Objective-C 库的 Swift “使用未声明的类型”

Alamofire v4 使用未声明的类型响应