iOS10でSwift3のpush通知設定(UNUserNotificationCenterを使う)
こんばんわ!断捨離が好きな人です。
iOS10からUser Notifications Frameworkが発表されていて、iOS9まで使っていたUIUserNotificationSettingsがdeprecatedになってしまった訳ですが、今日はその書き方です。要するにわたしのメモです。
AppDelegate.swift内で以下のように書きます。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler:{ (g, e) in if (e != nil) { print("err with notification auth: ", e.debugDescription) return } if (g) { UIApplication.shared.registerForRemoteNotifications() } }) return true }
今までは通知の許可設定を変数に入れてからそれを使ってregisterUserNotificationSettings
を呼び出すようにしていたものを、リクエストを送るインスタンスメソッド内でコールバックをハンドリング出来るようになったので安定感が増しました気がします。
簡単でしょ?そう、アイフォーンならね。