在我的应用程序中,我希望能够检查用户是否启用了通知。在 iOS 10 中,我使用委托中的检查来执行此操作。
此检查现已弃用,我想更新它,但我无法弄清楚在 iOS 11 中使用什么。
弃用警告如下所示:
当前用户通知设置'在 iOS 10.0 中已弃用:使用 用户通知框架的 -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] 和 -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]
我试图在此警告的帮助下更新代码,但我无法弄清楚。
如果有人可以建议无论如何获得这样的支票,那将有很大帮助。我一直在为iOS 10使用的代码如下,谢谢。
let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
print("Notifications are NOT enabled")
} else {
print("Notifications are enabled")
}
步骤 1 : import UserNotifications
步骤 2 :
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Notifications are allowed
}
else {
// Either denied or notDetermined
}
}
检查设置对象以获取更多信息。
第一步:
您必须将头文件添加为
import UserNotifications
我使用checkpushNotification方法来检查用户是否启用通知。 使用从 didFinishLaunchingWithOptions of AppDelegate 类调用此方法。 希望它能帮助你, 如果有任何问题,请在下面评论。
最后一步:
func checkPushNotification(){
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
switch setttings.authorizationStatus{
case .authorized:
print("enabled notification setting")
case .denied:
print("setting has been disabled")
case .notDetermined:
print("something vital went wrong here")
}
}
} else {
let isNotificationEnabled = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert)
if isNotificationEnabled{
print("enabled notification setting")
}else{
print("setting has been disabled")
}
}
}
如果您希望启用或禁用某些布尔输出,则应实现完成处理程序来解决它。
func checkPushNotification(checkNotificationStatus isEnable : ((Bool)->())? = nil){
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
switch setttings.authorizationStatus{
case .authorized:
print("enabled notification setting")
isEnable?(true)
case .denied:
print("setting has been disabled")
isEnable?(false)
case .notDetermined:
print("something vital went wrong here")
isEnable?(false)
}
}
} else {
let isNotificationEnabled = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert)
if isNotificationEnabled == true{
print("enabled notification setting")
isEnable?(true)
}else{
print("setting has been disabled")
isEnable?(false)
}
}
}
并称其为简单
self.checkPushNotification { (isEnable) in
print(isEnable)
// you know notification status.
}
你可以打电话给UIApplication.shared.isRegisteredForRemoteNotifications