检查 UILocalNotification 弃用后是否启用了用户通知
Check whether user notifications are enabled after UILocalNotification deprecation
在我的应用中,我希望能够检查用户是否启用了通知。在 iOS 10 中,我使用委托中的支票完成了此操作。
此检查现已弃用,我想更新它,但我不知道在 iOS 11.
中使用什么
弃用警告如下:
currentUserNotificationSettings' was deprecated in iOS 10.0: Use
UserNotifications Framework's -[UNUserNotificationCenter
getNotificationSettingsWithCompletionHandler:] and
-[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 class 的 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
在我的应用中,我希望能够检查用户是否启用了通知。在 iOS 10 中,我使用委托中的支票完成了此操作。
此检查现已弃用,我想更新它,但我不知道在 iOS 11.
中使用什么弃用警告如下:
currentUserNotificationSettings' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] and -[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 class 的 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