本地通知 - 最初拒绝后再次请求许可

时间:2016-02-01 09:56:35

标签: objective-c uilocalnotification

当应用首次启动时,它会要求用户发送本地通知的权限。

如果用户在发布时拒绝许可,我们将来如何再次申请许可?

例如,有没有办法在点击按钮时,我可以再次请求权限?

以下是我最初在发布时要求获得许可的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// are you running on iOS8?
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
    [application registerUserNotificationSettings:settings];
} 
else // iOS 7 or earlier
{
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
}
}

2 个答案:

答案 0 :(得分:2)

如果您第一次不允许本地通知,并且第二次使用上述代码:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]
{
    UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

    if (grantedSettings.types == UIUserNotificationTypeNone)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app-settings:"]];
    }
}

这将打开您的应用程序设置,您可以在其中找到启用/禁用本地通知和其他设置的设置。

希望这有帮助!

答案 1 :(得分:1)

您无法再次请求系统提示。您可以检测访问被拒绝的情况并自己显示提醒或显示将打开设置应用程序的按钮,以便更轻松地为用户更改设置。