在 Firebase 中使用推送通知时在 Swift 3 中收到警告?

Getting a warning in Swift 3 while using Push Notification with Firebase?

我正在为我的演示应用程序使用推送通知服务。使用 Firebase,我可以将通知发送到设备。但是我的日志消息中出现了一些警告。我怎样才能删除它?还有一件事,我如何在 Swift 3.

中使用更新后的 Xcode 8 获取设备令牌

Appdelegate 代码:

  import UIKit
  import Firebase
  import UserNotifications
  import FirebaseInstanceID
  import FirebaseMessaging

  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


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



    UINavigationBar.appearance().barTintColor = UIColor(red: 233.0/255.0, green: 86.0/255.0, blue: 53.0/255.0, alpha: 1.0)

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()


    UITabBar.appearance().backgroundColor = UIColor.whiteColor()
    // UITabBar.appearance().tintColor = UIColor.orangeColor()
     UITabBar.appearance().tintColor = UIColor(red: 233.0/255.0, green: 86.0/255.0, blue: 53.0/255.0, alpha: 1.0)

    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
            authOptions,
            completionHandler: {_,_ in })

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        // For iOS 10 data message (sent via FCM)
        //   FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    // [END register_for_notifications]

    // Use Firebase library to configure APIs
    FIRApp.configure()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(self.tokenRefreshNotification),
                                                     name: kFIRInstanceIDTokenRefreshNotification,
                                                     object: nil)

    return true
}

// [START receive_message]
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)

}
// [END receive_message]

// [START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
        print("InstanceID token: \(notification.object)")
    }
    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}
// [END refresh_token]


func application(application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)


}

// [START connect_to_fcm]
func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}
// [END connect_to_fcm]


func applicationWillResignActive(application: UIApplication) {

}

// [START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
    FIRMessaging.messaging().disconnect()
    print("Disconnected from FCM.")

}
// [END disconnect_from_fcm]

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {

connectToFcm()

}

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


}

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

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(center: UNUserNotificationCenter,
                            willPresentNotification notification: UNNotification,
                                                    withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}
}

//extension AppDelegate : FIRMessagingDelegate {
//    // Receive data message on iOS 10 devices.
//    func applicationReceivedRemoteMessage(remoteMessage:     FIRMessagingRemoteMessage) {
//        print("%@", remoteMessage.appData)
//    }
//}
  1. 首先在didFinishLaunchingWithOptions
  2. 中添加这个Observer
NSNotificationCenter.defaultCenter().addObserver(self,
                                                   selector: #selector(tokenRefreshNotification(_:)),
                                                   name: kFIRInstanceIDTokenRefreshNotification,
                                                   object: nil)
  1. 然后在Appdelegate.M中添加这个方法Class
func tokenRefreshNotification(notification: NSNotification) {

  let refreshedToken = FIRInstanceID.instanceID().token()
  print("InstanceID token: \(refreshedToken)")
  print("InstanceID token: \(notification.Object)")


  connectToFcm()
}
  1. 然后检查是否调用了此方法 (tokenRefreshNotification)?

在这个方法中你得到令牌ID