Swift2.3で記述したUserNotificationコード(下記)でuserNotificationCenterが呼ばれません。
基本的に同等(と思われる)Swift3では動作します。基本的というのは、Swift2.3 と Swift3 でシンタックスが異なり、Xcodeの補完に従って変更しています。ここで誤っているのかもしれませんが、、、
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 10.0, *) {
// iOS 10
let center = UNUserNotificationCenter.currentNotificationCenter()
center.requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { (granted, error) in
if error != nil {
return
}
if granted {
debugPrint("通知許可")
center.delegate = self
let content = UNMutableNotificationContent()
content.title = "Title"
content.subtitle = "Subtitle" // 新登場!
content.body = "Body"
content.sound = UNNotificationSound.defaultSound()
// 5秒後に発火
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond",
content: content,
trigger: trigger)
// ローカル通知予約
center.addNotificationRequest(request, withCompletionHandler: nil)
center.getPendingNotificationRequestsWithCompletionHandler { (requests: [UNNotificationRequest]) in
print(">>>>> \(requests)")
}
} else {
debugPrint("通知拒否")
}
})
} else {
// <= iOS 9
// let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
// UIApplication.shared.registerUserNotificationSettings(settings)
}
return true
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
// ...
let identifier = notification.request.identifier
debugPrint("userNotificationCenter \(identifier)")
}
}