【问题标题】:Display UILocalNotification request after user clicked "don't allow"用户点击“不允许”后显示 UILocalNotification 请求
【发布时间】:2017-01-30 22:03:49
【问题描述】:

我想允许我的用户在我的应用中禁用/启用本地通知。我知道当我为UILocalNotification 调用寄存器时,无论调用多少次寄存器,弹出窗口都只显示一次。如果用户稍后决定在我的应用中启用通知,有没有办法重置他们的答案并再次询问?

 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 适用于iOS 10+。

【问题讨论】:

    标签: ios swift notifications prompt


    【解决方案1】:

    您可以将用户发送到用户设备设置应用的应用设置页面,让他们选择加入 LocalNotification

    • 版本 10 之前的 iOS 不提供用户之前是否拒绝权限的信息。
    • 在您的应用中,您需要保存在 NSUserDefaults 或您已经请求权限的地方。
    • 每次需要权限时,首先检查权限是否可用。
    • 如果权限不可用并且您的应用之前没有询问过用户(基于上一步中保存的状态),您可以请求权限。
    • 如果您之前已向用户征求过许可(根据上一步中保存的状态),您会提示用户是否要允许许可,如果他们说“是”,则将其转发到设备设置应用程序。

    适用于 iOS

    这是一些查询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

    这是一些查询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)
            }
        }
    

    适用于 iOS 10 及更高版本

    检查您的应用是否曾要求授权,而不是在您的应用中保存变量,而是检查 UNNotificationSettings 类的 authorizationStatus 的值

    如果您的应用从未使用 requestAuthorization(options:completionHandler:) 方法请求授权,则此属性的值未确定。

    以及与此类的旧版本 (UIUserNotificationSettings) 类似的其他 UNNotificationSettings 对应方法请求权限或检查可用权限,如徽章、警报或声音。

    【讨论】:

    • 这个答案的一个主要问题是它在 Objective-C 中。尽管如此,这是一个很好的答案。这是一个在 Swift 中详细说明相同内容的答案:stackoverflow.com/questions/41303746/…
    • 感谢您的帮助。 UIApplicationOpenSettingsURLString 让我直接进入我的应用程序的通知设置!还要感谢@dfd 的链接,很高兴了解“权限启动”的想法。
    • 我为那个问题添加了书签,因为它的答案。总有一天它也会帮助我。很高兴它可能为您提供了另一种思考方式。
    【解决方案2】:

    不可以,您只能请求一次通知权限,该权限由操作系统管理。如果他们按Don't Allow,用户将不得不进入设置并手动更改权限。

    许多应用解决此问题的方法是在显示 iOS 对话框之前显示自定义“软”警报。这样,如果用户按下“不是现在”,您可以在未来某个时间显示您的自定义警报,并保留在用户准备启用通知(或任何其他权限)时显示 iOS 对话框。

    TLDR;不,在初始对话框之后(包括初始对话框)由用户管理他们的通知首选项。

    【讨论】:

    • 这很有帮助,谢谢。可惜它必须是那样的。
    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2013-11-22
    • 2019-10-13
    相关资源
    最近更新 更多