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

Local Notifications - asking for permission again after initially rejected

当应用首次启动时,它会请求用户允许发送本地通知。

如果用户在启动时拒绝权限,我们以后如何再次请求权限?

例如,有没有一种方法可以在单击按钮时再次请求权限?

这是我在启动时最初请求许可的代码:

- (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];
}
}

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

如果你第一次禁止本地通知,第二次使用上面提到的代码:

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

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

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

希望对您有所帮助!