如何知道是否已显示推送通知权限警报

时间:2014-08-12 12:22:30

标签: ios push-notification apple-push-notifications

我想实现一个自定义屏幕,通知我的用户我为什么要请求推送通知权限。在他们按下该自定义屏幕中的按钮后,我将使用[[UIApplication sharedApplication] registerForRemoteNotificationTypes:

显示iOS推送通知权限对话框

如果用户尚未看到推送通知权限对话框,我只想显示此自定义屏幕一次。我无法使用[[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone,因为如果用户决定不允许推送通知,这也会返回“无”。

任何想法?

3 个答案:

答案 0 :(得分:4)

您可以使用NSUserDefaults:

#define kPushNotificationRequestAlreadySeen @"PushNotificationRequestAlreadySeen"

if(![[NSUserDefaults standardUserDefaults] boolForKey:kPushNotificationRequestAlreadySeen]) {

    // Notify the user why you want to have push notifications
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:... delegate:self ...];
    [alertView show];

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kPushNotificationRequestAlreadySeen];
}
else {
    // Already allowed -> register without notifying the user
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

并且

- (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)index {
    // Actually registering to push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

答案 1 :(得分:1)

这只是一种解决方法,但在大多数情况下都适用。

当iOS提示用户允许推送通知时,会调用applicationWillResignActive。

调用registerForRemoteNotificationTypes后...启动一个计时器(或dispatch_after),它将向用户显示一条新警报,说明他们需要使用“设置”应用程序启用通知。然后在applicationWillResignActive中取消计时器。这样,解释警报仅显示是否从未调用applicationWillResignActive。

我看到的唯一问题是,如果应用程序在计时器运行的确切时间因其他原因而重新启动...但这会使您处于已经存在并且已经存在的情况中在大多数情况下工作的优势。

答案 2 :(得分:-1)

我发现的解决方案有点像黑客,但它确实有效。 您需要为两个不同的notificationSettings调用registerUserNotificationSettings - 一个没有notificationCategory,另一个有notificationCategory:

        //Request notification permission
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    //Request notification permission again, but with a category with no actions
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"com.xyz.markNotificationPopupShownCategoryIdentifier";

    UIUserNotificationSettings *notificationSettingsWithCategory = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObject:category]];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettingsWithCategory];

app委托中的didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings方法将被调用两次,无论用户在权限通知中的答案如何,在第二次调用之后,当前通知设置将包含该类别。只要类别计数大于0,您就可以确定已显示通知权限对话框:

if ([UIApplication sharedApplication].currentUserNotificationSettings.categories.count > 0) {
    NSLog(@"Notifications permission has been asked");
} else {
    NSLog(@"Notifications permission hasn't been asked");
}