Sta*_*108 19 notifications ios
我在我的应用中有推送通知.每当启动应用程序时,我都想检查用户是否为我的应用程序启用了推送通知.
我是这样做的:
let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
if notificationType == UIUserNotificationType.None {
print("OFF")
} else {
print("ON")
}
Run Code Online (Sandbox Code Playgroud)
如果用户禁用推送通知,有没有办法从我的应用程序激活此功能?
或者有没有其他选择将用户发送到推送通知设置(设置 - 通知 - AppName)?
sal*_*aha 19
无法从应用程序更改设置.但您可以使用此代码将用户引导至应用程序特定的系统设置.
extension UIApplication {
class func openAppSettings() {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
}
Run Code Online (Sandbox Code Playgroud)
更新了Swift 3.0
extension UIApplication {
class func openAppSettings() {
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*ter 15
检查是否已为您的应用启用了推送通知已大幅改变Swift 3.如果您使用的是Swift 3,请使用此代替上述示例.
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if(settings.authorizationStatus == .authorized)
{
print("Push authorized")
}
else
{
print("Push not authorized")
}
}
Run Code Online (Sandbox Code Playgroud)
以下是Apple有关如何进一步优化支票的文档:https://developer.apple.com/reference/usernotifications/unnotificationsettings/1648391-authorizationstatus
这是Swift 3版本,您可以在其中检查是启用还是禁用通知.
let notificationType = UIApplication.shared.currentUserNotificationSettings?.types
if notificationType?.rawValue == 0 {
print("Disabled")
} else {
print("Enabled")
}
Run Code Online (Sandbox Code Playgroud)