Ios 检查UILocalNotification弃用后是否启用了用户通知
在我的应用程序中,我希望能够检查用户是否启用了通知。在iOS 10中,我使用签入委托来完成此操作 此检查现在已被弃用,我想对其进行更新,但我不知道在iOS 11中使用什么 弃用警告如下所示: currentUserNotificationSettings'在iOS 10.0中已被弃用:使用 用户通知框架的-[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:]和 -[UnuseNotificationCenter getNotificationCategoriesWithCompletionHandler:] 我试图借助此警告更新代码,但我无法理解 如果有人能建议,无论如何,得到这样一个工作的检查,这将有很大帮助。我在iOS 10上使用的代码如下,谢谢Ios 检查UILocalNotification弃用后是否启用了用户通知,ios,cocoa-touch,usernotifications,Ios,Cocoa Touch,Usernotifications,在我的应用程序中,我希望能够检查用户是否启用了通知。在iOS 10中,我使用签入委托来完成此操作 此检查现在已被弃用,我想对其进行更新,但我不知道在iOS 11中使用什么 弃用警告如下所示: currentUserNotificationSettings'在iOS 10.0中已被弃用:使用 用户通知框架的-[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:]和 -[UnuseNotificationCen
let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
print("Notifications are NOT enabled")
} else {
print("Notifications are enabled")
}
步骤1:
导入用户通知
步骤2:
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Notifications are allowed
}
else {
// Either denied or notDetermined
}
}
有关更多信息,请检查设置对象。第一步:- 您必须将头文件添加为
import UserNotifications
我使用了checkpushNotification方法来检查用户是否启用通知。使用从AppDelegate类的didFinishLaunchingWithOptions调用此方法。希望它能帮助你,如果有任何问题,请在下面评论
最后一步:-
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
,对于早期版本?@Josh我已经更新了上面的早期版本。如果它不起作用,请在下面进行评论。@Amrit Tiwari这是否意味着如果isNotificationEnabled=false,它已被拒绝?它的工作方式是否与您示例中前面的switch情况相同?isNotificationEnabled=false,这适用于用户拒绝和未确定的两种情况。您好,Amrit,您能告诉我如何将函数转换为throw Bool()作为结果吗?例如->Bool()?在iOS 10之前的版本中如何处理此问题?您必须通过currentUserNotificationSettings
方法从UIApplication.shared获取UIUserNotificationSettings
对象。如何将其转换为抛出Bool()的func?