使用未解析的标识符“ InstanceIDAPNSTokenType”

时间:2018-10-04 20:48:40

标签: ios swift firebase

无法解析的标识符的问题之前已经得到解决,我完成了从回购到安装(从pod中删除firebase)并使用pod中的firebase进行重新安装的所有步骤...等等,但仍然无法解决此问题。我更新广告连播后,就会产生此问题以及其他问题。该问题发生在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)
}

2 个答案:

答案 0 :(得分:3)

确保您使用的是Firebase 5.x,并通过Firebase Messaging类(而不是InstanceID)使用设置APNS令牌。

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

答案 1 :(得分:2)

我删除了以下代码

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

并用

替换
Messaging.messaging().apnsToken = deviceToken

经过测试,工作正常!