Swift 3 - Firebase推送通知,我该怎么办?

iam*_*rak 3 ios firebase swift swift3 firebase-notifications

我喜欢下面的Swift 2.但它在Swift 3中不起作用.我怎么能提供这个?如果有人解释这将是伟大的.

didFinishLaunchingWithOptions

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    print("Meesage ID \(userInfo["gcm_message_id"]!)")
    print(userInfo)

}
Run Code Online (Sandbox Code Playgroud)

我可以做简单的本地通知,但是我无法从Firebase远程推送通知.

我试过了

UNUserNotificationCenter.currentNotificationCenter().delegate = self

UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Badge, .Sound]) { (success, error:NSError?) in

        if success {
            print("Notification access true!")
            application.registerForRemoteNotifications()
        }
        else {
            print(error?.localizedDescription)
        }

}
Run Code Online (Sandbox Code Playgroud)

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    print("Meesage ID \(userInfo["gcm_message_id"]!)")
    print(userInfo)

}
Run Code Online (Sandbox Code Playgroud)

仍然无法正常工作.

Cal*_*lam 9

AppDelegate方法名称稍有变化,并引入了UserNotifications框架.您必须将此框架用于iOS 10及更高版本中的通知,因为其他方法已被弃用.

import UserNotifications

...

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }

        application.registerForRemoteNotifications()

        return true
    }

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

        print("Message ID \(userInfo["gcm.message_id"]!)")
        print(userInfo)
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

在Xcode 8/Swift 3.0中注册推送通知?