ios - 使用未解析的标识符 'InstanceIDAPNSTokenType'

标签 ios swift firebase

未解析标识符的问题之前已经解决,我完成了从 repo 到安装(从 pod 中删除 firebase)和在 pod 中重新安装 firebase 的所有步骤...等等,但它仍然没有解决这个问题。这个问题连同其他问题是在我更新 pod 后产生的。该问题出现在 AppDelegate 中的一个函数中。

这是我的AppDelegate.swift

import UIKit
import Firebase
import FirebaseMessaging
import FirebaseAuth
import FirebaseDatabase
import UserNotifications

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

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    FirebaseApp.configure()

    // Messaging Delegate
    Messaging.messaging().delegate = self

    // Notification delegate
    UNUserNotificationCenter.current().delegate = self

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

        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
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

    print("Registered with FCM with token:", fcmToken)
    [Messaging.messaging().fcmToken!:Messaging.messaging().fcmToken as AnyObject]
}

//
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.sandbox)
}

// Listen for user notifications - basically show the notification while a user is on foreground mode
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(.alert)

    // Reset badge number to zero
    if let userID = Auth.auth().currentUser?.uid {

        let dbRef = Database.database().reference()
        dbRef.child("Users Info").child(userID).updateChildValues(["badge":"0"], withCompletionBlock: { (err, ref) in
            if err != nil {
                print(err!)
                return
            }
        })

    }


}

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


    if let mainTabBarController = window?.rootViewController as? MainTabBarControllerViewController {

        mainTabBarController.selectedIndex = 1

        mainTabBarController.tabBar.items?[1].badgeValue = nil


    }

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    completionHandler(.newData)

    if let aps = userInfo["aps"] as? [String:Any] {
        let badgeNumber = aps["badge"] as! Int

        if let mainTabBarController = window?.rootViewController as? MainTabBarControllerViewController {

            mainTabBarController.tabBar.items?[1].badgeValue = String(Int(badgeNumber))

        }

    }

}



func applicationWillResignActive(_ application: UIApplication) {

}

func applicationDidEnterBackground(_ application: UIApplication) {


}

func applicationWillEnterForeground(_ application: UIApplication) {

}

func applicationDidBecomeActive(_ application: UIApplication) {

    if let mainTabBarController = window?.rootViewController as? MainTabBarControllerViewController {

        if  mainTabBarController.selectedIndex == 1 {

            // Reset Requests tab's badge
            mainTabBarController.tabBar.items?[1].badgeValue = nil
        }

    }


    application.applicationIconBadgeNumber = 0

    // Reset badge number to zero
    if let userID = Auth.auth().currentUser?.uid {

        let dbRef = Database.database().reference()
        dbRef.child("Users Info").child(userID).updateChildValues(["badge":"0"], withCompletionBlock: { (err, ref) in
            if err != nil {
                print(err!)
                return
            }
        })

    }


}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

这是从 AppDelegate 中弹出错误的地方

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.sandbox)
}

最佳答案

确保您使用的是 Firebase 5.x,并使用 Firebase Messaging 类而不是 InstanceID 设置 APNS token 。

参见 https://firebase.google.com/docs/cloud-messaging/ios/client

关于ios - 使用未解析的标识符 'InstanceIDAPNSTokenType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52654934/

相关文章:

swift - 编辑按钮没有任何功能,单元格左侧没有带减号的红色圆圈

ios - Unity VR LandscapeLeft 在 iOS 中不起作用

ios - 应用程序本地化显示键而不是 iOS 中的值

ios - 将 Dlib 添加到我的 Xcode 项目但未找到 .h 文件

ios - 在 Swift playground 中禁用某些代码行的运行

javascript - firebase.database() 不是函数,使用当前的 firebase 实时数据库库

ios - 无法将类型 '() -> Void' 的值分配给类型 '(() -> Void)!'

json - 从 JSON 字符串中的二维数组检索值

android - Firebase set value 重写 children- Android

javascript - 如何以 Angular 检索对象详细信息?