SwiftやOSバージョンによって混乱するのでメモ。AppDelegate.swiftもしくは、プッシュ許可ダイアログを出したいViewController上で下記コードを記載します。このときこのようにUNUserNotificationCenterDelegateを呼び出す必要があります。
UNUserNotificationCenterDelegateを使う
1
class YourViewController: UIViewController, UNUserNotificationCenterDelegate {
任意の場所に記載
1 |
class YourViewController: UIViewController, UNUserNotificationCenterDelegate { |
任意の場所に記載
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) UIApplication.shared.registerUserNotificationSettings(settings) } UIApplication.shared.registerForRemoteNotifications() |