in mpush_ios_swift_demo/mpush_ios_swift_demo/AppDelegate.swift [38:67]
func registerAPNs(_ application: UIApplication) {
if #available(iOS 10, *) {
// iOS 10+
let center = UNUserNotificationCenter.current()
// 创建category,并注册到通知中心
createCustomNotificationCategory()
center.delegate = self
// 请求推送权限
center.requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if (granted) {
// User authored notification
print("User authored notification.")
// 向APNs注册,获取deviceToken
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
} else {
// User denied notification
print("User denied notification.")
}
})
} else if #available(iOS 8, *) {
// iOS 8+
application.registerUserNotificationSettings(UIUserNotificationSettings.init(types: [.alert, .badge, .sound], categories: nil))
application.registerForRemoteNotifications()
} else {
// < iOS 8
application.registerForRemoteNotifications(matching: [.alert,.badge,.sound])
}
}