检查UILocalNotification弃用后是否启用用户通知
问题描述:
在我的应用中,我希望能够检查用户是否启用了通知。在iOS 10中,我使用委托中的检查来完成此操作。检查UILocalNotification弃用后是否启用用户通知
此检查现在已经过时,我希望更新它,但我想不出在iOS的11
弃用警告如下的内容:
currentUserNotificationSettings'被弃用iOS的10.0:使用 UserNotifications Framework的 - [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方法来检查用户是否启用通知与否。使用从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")
}
}
}
你怎么预iOS的10处理呢? – Josh
您必须通过'currentUserNotificationSettings'方法从** UIApplication.shared **中获取'UIUserNotificationSettings'对象。 – McNight
非常感谢:) – Josh