用户点击后显示 UILocalNotification 请求 "don't allow"
Display UILocalNotification request after user clicked "don't allow"
我想允许我的用户从我的应用程序中 disable/enable 本地通知。我知道当我为 UILocalNotification
调用 register 时,无论调用多少次 register,弹出窗口只显示一次。如果用户以后决定在我的应用程序中启用通知,有没有办法重置他们的答案并再次询问?
application.registerUserNotificationSettings(
UIUserNotificationSettings( forTypes: [.Alert, .Badge, .Sound],
categories: nil ) )
我可以将他们的答案重置为 registerUserNotificationSettings()
吗?像
// if user clicked enable notifications
let grantedSettings = application.currentUserNotificationSettings()
// reset grantedSettings
// call registerUserNotificationSettings() again
PS:我知道现在推荐UNNotificationRequest
,但我想支持iOS 9.0,我读到UNNotificationRequest
is for iOS 10 +.
不可以,您只能申请一次通知权限,权限由 OS 管理。如果他们按 Don't Allow
,用户将必须进入设置并手动更改权限。
许多应用解决此问题的方法是在显示 iOS 对话框之前显示自定义 'soft' 警报。这样,如果用户按下 'not now',您可以在将来的某个时间显示您的自定义警报,并保留在用户准备好启用通知(或任何其他权限)时显示 iOS 对话框。
TLDR;不,在初始对话之后并包括初始对话之后,由用户管理他们的通知首选项。
您可以将用户转到用户设备设置应用的应用设置页面,让他们opt-in进入 LocalNotification
- iOS 10 之前的版本不提供用户之前是否拒绝过权限的信息。
- 在您的应用中,您需要保存在 NSUserDefaults 或您已经请求许可的地方。
- 每次需要权限时,先检查权限是否可用。
- 如果权限不可用并且您的应用程序之前没有询问过用户(基于先前步骤中保存的状态),您将请求权限。
- 如果您之前请求过用户的许可(基于先前步骤中保存的状态),您会提示用户是否要允许许可,如果他们说 'Yes' 将他们转发到设备设置应用程序。
Objective-C for iOS <= 10(在 iOS 10 中弃用)
这是一些用于查询的代码片段 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.
此代码片段展示了如何将用户发送到设备设置页面。
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
Swift 3 for iOS <= 10(在 iOS 10 中弃用)
这是一些用于查询的代码片段 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
}
此代码片段展示了如何将用户发送到设备设置页面。
if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(urlStr) {
UIApplication.shared.openURL(urlStr)
}
}
iOS10岁及以上
检查您的应用是否曾请求过授权,而不是在您的应用中保存变量,而是检查 UNNotificationSettings
class
的 authorizationStatus
的值
The value of this property is notDetermined if your app has never requested authorization using the requestAuthorization(options:completionHandler:) method.
连同其他 UNNotificationSettings
类似于此 class ( UIUserNotificationSettings
) 旧版本的对应方法请求权限或检查可用的权限,如徽章、警报或声音。
我想允许我的用户从我的应用程序中 disable/enable 本地通知。我知道当我为 UILocalNotification
调用 register 时,无论调用多少次 register,弹出窗口只显示一次。如果用户以后决定在我的应用程序中启用通知,有没有办法重置他们的答案并再次询问?
application.registerUserNotificationSettings(
UIUserNotificationSettings( forTypes: [.Alert, .Badge, .Sound],
categories: nil ) )
我可以将他们的答案重置为 registerUserNotificationSettings()
吗?像
// if user clicked enable notifications
let grantedSettings = application.currentUserNotificationSettings()
// reset grantedSettings
// call registerUserNotificationSettings() again
PS:我知道现在推荐UNNotificationRequest
,但我想支持iOS 9.0,我读到UNNotificationRequest
is for iOS 10 +.
不可以,您只能申请一次通知权限,权限由 OS 管理。如果他们按 Don't Allow
,用户将必须进入设置并手动更改权限。
许多应用解决此问题的方法是在显示 iOS 对话框之前显示自定义 'soft' 警报。这样,如果用户按下 'not now',您可以在将来的某个时间显示您的自定义警报,并保留在用户准备好启用通知(或任何其他权限)时显示 iOS 对话框。
TLDR;不,在初始对话之后并包括初始对话之后,由用户管理他们的通知首选项。
您可以将用户转到用户设备设置应用的应用设置页面,让他们opt-in进入 LocalNotification
- iOS 10 之前的版本不提供用户之前是否拒绝过权限的信息。
- 在您的应用中,您需要保存在 NSUserDefaults 或您已经请求许可的地方。
- 每次需要权限时,先检查权限是否可用。
- 如果权限不可用并且您的应用程序之前没有询问过用户(基于先前步骤中保存的状态),您将请求权限。
- 如果您之前请求过用户的许可(基于先前步骤中保存的状态),您会提示用户是否要允许许可,如果他们说 'Yes' 将他们转发到设备设置应用程序。
Objective-C for iOS <= 10(在 iOS 10 中弃用)
这是一些用于查询的代码片段 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.
此代码片段展示了如何将用户发送到设备设置页面。
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
Swift 3 for iOS <= 10(在 iOS 10 中弃用)
这是一些用于查询的代码片段 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
}
此代码片段展示了如何将用户发送到设备设置页面。
if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(urlStr) {
UIApplication.shared.openURL(urlStr)
}
}
iOS10岁及以上
检查您的应用是否曾请求过授权,而不是在您的应用中保存变量,而是检查 UNNotificationSettings
class
authorizationStatus
的值
The value of this property is notDetermined if your app has never requested authorization using the requestAuthorization(options:completionHandler:) method.
连同其他 UNNotificationSettings
类似于此 class ( UIUserNotificationSettings
) 旧版本的对应方法请求权限或检查可用的权限,如徽章、警报或声音。