ios - 如何在 iOS 中打开和关闭通知?

标签 ios swift unusernotificationcenter usernotifications

我正在尝试在 iOS 11 应用程序中实现设置屏幕,我需要一个 UISwitch 来控制用户通知。当设置为关闭时,我想放弃通知权限,当设置为打开时,我想请求权限(标准对话框要求用户允许发送她的通知)。

为了请求许可,我找到了以下代码:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}

但是,如果我在系统设置中关闭该应用程序的通知,此代码不会弹出带有请求的对话框,它只是简单地在 granted 中返回 false。

我找不到任何关于如何放弃权限的信息。

关于如何解决问题的任何提示?有没有可能,或者 Apple 认为这个任务应该只留给系统设置?

最佳答案

In iOS Turn on/off permission push notification appears only one time. So in order to achieve that you need to do some tweak like you can check first whether your notification is enabled or not.

func pushEnabledAtOSLevel() -> Bool {
 guard let currentSettings = UIApplication.shared.currentUserNotificationSettings?.types else { return false }
 return currentSettings.rawValue != 0
}

After that you can create your customize popup with TurnON/Off button and navigate to System setting page where user can enable that option accordingly

if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.openURL(appSettings as URL)
}

关于ios - 如何在 iOS 中打开和关闭通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376979/

相关文章:

ios - hkworkout totalenergyburned 与 HKQuantityTypeIdentifierActiveEnergyBurned 不相关

ios - 使用 UIImage 全屏需要什么类型/尺寸的图片?

swift - 如何修复azure devops构建代理中的 ' *pod* does not support provisioning profiles'

swift - 如何修复被阻挡的 View 过渡到增强现实 View

objective-c - iOS 11 与 bundleProxy 崩溃!= 使用 UNUserNotificationCenter 时出现 nil 错误

ios - 在 Xcode 8/Swift 3.0 中注册推送通知?

ios - View 与另一个 View 重叠

iphone - removeFromSuperview 完成后,会显示一个白色屏幕

ios - 如何在 Interface Builder 中实现这种布局?

ios - authorizationStatus 返回一个 bool 值