这个
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
...
// code here
})
询问用户是否接受实际将显示弹出窗口的接收通知,但这(用于非本地推送通知)
UIApplication.shared.registerForRemoteNotifications()
根据
Docs
调用此方法以启动与Apple的注册过程
推送通知服务。如果注册成功,应用程序将调用
你的应用委托对象
应用程序:DidRegisterForRemotNotificationsWithDeviceToken:方法
并将设备令牌传递给它。
//
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()