用户点击后显示UILocalNotification请求"不允许"

时间:2017-01-30 22:03:49

标签: ios swift notifications prompt

我想允许我的用户在我的应用中禁用/启用本地通知。我知道当我为UILocalNotification调用注册时,无论调用多少次注册,弹出窗口都只会显示一次。有没有办法重置他们的答案并再次询问用户以后是否决定从我的应用程序中启用通知?

Enable/Disable Notifications Toggle

 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 +。

2 个答案:

答案 0 :(得分:2)

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

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

适用于iOS的Objective-C< = 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];
                }

适用于iOS的Swift 3< = 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)
        }
    }

适用于iOS 10及以上版本

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

  

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

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

答案 1 :(得分:1)

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

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

TLDR;不,用户可以在初始对话框之后管理他们的通知偏好。

相关问题