检查Swift中推送通知的用户设置 [英] Check user settings for push notification in Swift
本文介绍了检查Swift中推送通知的用户设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用中有推送通知。每当应用程序启动时,我都想检查用户是否为我的应用程序启用了推送通知。
I have push notifications in my app. Whenever the app is launched, I would like to check whether the user has enabled push notification for my application.
我这样做:
let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
if notificationType == UIUserNotificationType.None {
print("OFF")
} else {
print("ON")
}
如果用户禁用推送通知,有没有办法从我的应用程序?
If push notifications are disabled by the user, is there any way to activate this from my app?
或者有没有其他选择将用户发送到推送通知设置(设置 - 通知 - AppName)?
Or is there any alternative to send user to the push notification settings (Settings - Notifications - AppName)?
推荐答案
无法从应用程序更改设置。但您可以使用此代码将用户引导至应用程序特定的系统设置。
There is no way to change settings from the app. But you can lead user to application specific system settings using this code.
extension UIApplication {
class func openAppSettings() {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
}
更新为Swift 3.0
Updated for Swift 3.0
extension UIApplication {
class func openAppSettings() {
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}
}
这篇关于检查Swift中推送通知的用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文