问题描述
我想允许我的用户在我的应用中禁用/启用本地通知.我知道当我为 UILocalNotification
调用 register 时,无论调用 register 多少次,弹出窗口只会显示一次.如果用户稍后决定在我的应用内启用通知,是否可以重置他们的答案并再次询问?
application.registerUserNotificationSettings(UIUserNotificationSettings( forTypes: [.Alert, .Badge, .Sound],类别:无 ) )
我可以重置他们对 registerUserNotificationSettings()
的回答吗?类似的东西
//如果用户点击启用通知让 grantSettings = application.currentUserNotificationSettings()//重置授予的设置//再次调用 registerUserNotificationSettings()
PS:我知道现在推荐使用 UNNotificationRequest
,但我想支持 iOS 9.0 并且我读到 UNNotificationRequest
适用于 iOS 10+.
您可以将用户发送到用户设备设置应用的应用设置页面,让他们选择加入 LocalNotification
- 版本 10 之前的 iOS 不提供用户之前是否拒绝过权限的信息.
- 在您的应用中,您需要将其保存在 NSUserDefaults 或您已经请求许可的地方.
- 每次需要权限时,首先检查权限是否可用.
- 如果权限不可用,并且您的应用之前未询问用户(根据之前步骤中保存的状态),您将请求权限.
- 如果您之前曾向用户征求过许可(基于上一步中保存的状态),您会提示用户是否允许许可,如果他们说是",则将他们转发到设备设置应用.
iOS 的 Objective-C
这是一些用于查询UIUserNotificationSettings
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]if (currentSettings.types == UIUserNotificationTypeNone)//权限不存在,用户拒绝或以前从未被询问过if (!(currentSettings.types & (UIUserNotificationTypeAlert | UIUserNotificationTypeSound)))//这意味着您的应用没有权限警报和播放带有通知的声音.
此代码片段展示了如何将用户发送到设备设置页面.
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];if ([[UIApplication sharedApplication] canOpenURL:url]) {[[UIApplication sharedApplication] openURL:url];}
Swift 3 for iOS
这是一些用于查询UIUserNotificationSettings
let currentSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], Categories: nil)如果 currentSettings.types.isEmpty {//这里你决定是否用新的授权请求提示用户//或根据您存储的变量将用户发送到设置应用程序}
此代码片段展示了如何将用户发送到设备设置页面.
if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {如果 UIApplication.shared.canOpenURL(urlStr) {UIApplication.shared.openURL(urlStr)}}
适用于 iOS 10 及更高版本
不要在你的应用程序中保存一个变量来检查你的应用程序是否曾经要求过授权,而是检查 UNNotificationSettings
类的 authorizationStatus
的值
如果您的应用从未使用 requestAuthorization(options:completionHandler:) 方法请求授权,则此属性的值未确定.
连同其他 UNNotificationSettings
对应方法,类似于此类的旧版本 (UIUserNotificationSettings
) 请求权限或检查可用的权限,如徽章、警报或声音.>
I want to allow my users to disable/enable local notifications from within my app. I know that when I call register for UILocalNotification
, the pop-up is only shown once, no matter how many times register is called. Is there a way to reset their answer and ask it again if the user later decides to enable notifications from within my app?
application.registerUserNotificationSettings(
UIUserNotificationSettings( forTypes: [.Alert, .Badge, .Sound],
categories: nil ) )
Can I reset their answer to registerUserNotificationSettings()
? Something like
// if user clicked enable notifications
let grantedSettings = application.currentUserNotificationSettings()
// reset grantedSettings
// call registerUserNotificationSettings() again
PS: I know that UNNotificationRequest
is recommended now, but I want to support iOS 9.0 and I read UNNotificationRequest
is for iOS 10+.
You can send users to your app's settings page of user's device setting app, letting them to opt-in into LocalNotification
- iOS earlier than version 10 dos not provide information if user has denied permission before or not.
- In your app you need to save in NSUserDefaults or somewhere that you have already asked the permission.
- Every time you require permission, first check if permission is available or not.
- If permission is not available and your app has not asked user before ( based on the saved status in prior step ) you go for requesting permission.
- If you have asked the permission from user before ( based on the saved status in prior step ) you prompt users if they want to allow permission, if they say 'Yes' forward them to the device setting app.
Objective-C for iOS <= 10 (Deprecated in iOS 10)
This is some code snip for inquiring UIUserNotificationSettings
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]
if (currentSettings.types == UIUserNotificationTypeNone) // Permission does not exist either user denied or never been asked before
if (!(currentSettings.types & (UIUserNotificationTypeAlert | UIUserNotificationTypeSound))) // This means your app does not have permission alert and to play sounds with notification.
This code snip shows how to send users to device setting page.
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
Swift 3 for iOS <= 10 (Deprecated in iOS 10)
This is some code snip for inquiring UIUserNotificationSettings
let currentSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
if currentSettings.types.isEmpty {
// Here you decide whether to prompt user with new authorization request
// or send user to setting app based on your stored variable
}
This code snip shows how to send users to device setting page.
if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(urlStr) {
UIApplication.shared.openURL(urlStr)
}
}
For iOS 10 and above
instead of saving a variable in your app the check whether your app ever asked for authorization or not, check the value of authorizationStatus
of UNNotificationSettings
class
along with other UNNotificationSettings
counterpart methods similar to older version of this class ( UIUserNotificationSettings
) request permission or check for the permissions available like badge, alert or sound.
这篇关于用户点击“不允许"后显示 UILocalNotification 请求;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!