这个
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
...
// code here
})
询问用户他是否接受实际会显示弹出窗口的接收通知,但这(用于非本地推送通知)
UIApplication.shared.registerForRemoteNotifications()
根据文档
调用此方法以使用 Apple Push Notification 服务启动注册过程。如果注册成功,应用程序会调用您的应用程序委托对象的 application:didRegisterForRemoteNotificationsWithDeviceToken: 方法并将设备令牌传递给它。
//
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()