Registering or the permissions granted for Push Notifications in Xcode 8/Swift 3.0

Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate.swift
Request user permission

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


    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }
    application.registerForRemoteNotifications()
    return true
}


Getting device token

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

let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)


}


In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

    print("i am not available in simulator \(error)")

}


In case if you need to know the permissions granted

UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in

        switch setttings.soundSetting{
        case .enabled:

            print("enabled sound setting")

        case .disabled:

            print("setting has been disabled")

        case .notSupported:
            print("something vital went wrong here")
        }
    }
2017-05-23 17:02 コメント
已邀请:

ログインまたは登録してから回答してください

lujian
lujian

Javaが得意です。

@ステータス

  • 更新: 2017-05-23 17:02
  • 閲覧: 4293
  • フォロー: 0
EXIT フルスクリーンモード 回答