如何判断用户是否在iOS 9或iOS 10上启用了远程通知?
如果用户没有允许或单击了No,我想切换一条消息,询问他们是否要启用通知.
如何判断用户是否在iOS 9或iOS 10上启用了远程通知?
如果用户没有允许或单击了No,我想切换一条消息,询问他们是否要启用通知.
IOS 10使用UNUserNotificationCenter
后更新了答案.
首先你需要import UserNotifications
美元然后
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { permission in
switch permission.authorizationStatus {
case .authorized:
print("User granted permission for notification")
case .denied:
print("User denied notification permission")
case .notDetermined:
print("Notification permission haven't been asked yet")
case .provisional:
// @available(iOS 12.0, *)
print("The application is authorized to post non-interruptive user notifications.")
case .ephemeral:
// @available(iOS 14.0, *)
print("The application is temporarily authorized to post notifications. Only available to app clips.")
@unknown default:
print("Unknow Status")
}
})
这段代码将一直工作到iOS9,对于iOS10使用上面的代码片段.
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}