简体   繁体   English

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

[英]How to know if the push notification permission alert has already been shown

I want to implement a custom screen that informs my users why I'm about to ask for push notification permissions. 我想实现一个自定义屏幕,通知我的用户我为什么要请求推送通知权限。 After they press a button in that custom screen I present the iOS push notification permission dialog with [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 在他们按下该自定义屏幕中的按钮后,我使用[[UIApplication sharedApplication] registerForRemoteNotificationTypes:呈现iOS推送通知权限对话框[[UIApplication sharedApplication] registerForRemoteNotificationTypes:

I only want to show this custom screen once if the user hasn't already seen the push notification permission dialog. 如果用户尚未看到推送通知权限对话框,我只想显示此自定义屏幕一次 I cannot use [[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone as this will also return 'none' if the user decided to not allow push notifications. 我不能使用[[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone因为如果用户决定不允许推送通知,这也将返回'none'。

Any ideas anyone? 任何人的想法?

You could use NSUserDefaults : 您可以使用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];
}

And

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

This is just a work-around but will work in the majority of cases. 这只是一种解决方法,但在大多数情况下都适用。

When iOS prompts the user to allow push notifications, applicationWillResignActive is called. 当iOS提示用户允许推送通知时,将调用applicationWillResignActive。

After calling registerForRemoteNotificationTypes...start a timer (or dispatch_after) that will show a new alert to the user explaining that they'll need to use the Settings app to enable notifications. 在调用registerForRemoteNotificationTypes后...启动一个计时器(或dispatch_after),它将向用户显示一个新警报,说明他们需要使用Settings应用程序启用通知。 Then in applicationWillResignActive cancel the timer. 然后在applicationWillResignActive中取消计时器。 This way, the explanation alert will only show if applicationWillResignActive is never called. 这样,解释警报仅显示是否从未调用applicationWillResignActive。

The only problem I see with this is if the app resigns active for other reasons at the exact time that the timer is going...but this puts you in no worse a situation that you're already in and has the advantage of working in the majority of cases. 我看到的唯一问题是,如果应用程序在计时器运行的确切时间因其他原因而重新启动...但这会让您处于一种情况,即您已经拥有并且具有工作的优势大多数情况。

The solution that I've found is a bit of a hack, but it works. 我发现的解决方案有点像黑客,但它确实有效。 You need to call registerUserNotificationSettings for two different notificationSettings - one without a notificationCategory and one with a notificationCategory: 您需要为两个不同的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];

The didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings method in the app delegate will be called two times, and irrespective of the user's answer in the permission notification, after the second call, the current notification settings will contain the category. 应用程序委托中的didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings方法将被调用两次,无论用户在权限通知中的答案如何,在第二次调用之后,当前通知设置将包含该类别。 As long as the category count is greater than 0, you can know for sure that the notifications permission dialog has been shown: 只要类别计数大于0,您就可以确定已显示通知权限对话框:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查是否已单击“询问推送通知”权限警报 - How to check if “Ask for push notifications” permission alert has been clicked on 删除并重新安装应用后,为推送通知权限重新生成iOS系统警报 - Regenerate iOS system alert for push notification permission AFTER app has been deleted and reinstalled 知道过去是否已显示iOS上的推送通知权限提示 - Knowing if push notifications permission prompt on iOS has been shown in the past 在UITextView textViewDidBeginEditing中显示键盘后如何显示警报 - How to show alert after keyboard has been shown in UITextView textViewDidBeginEditing ECSlidingViewController-如何知道菜单何时显示? - ECSlidingViewController - How do you know when the menu has been shown? 如何知道UIScrollView显示了哪个UIView索引或标签? - How to know which UIView index or tag has been shown by UIScrollView? 如何知道locationManager.requestAlwaysAuthorization()是否已被询问 - How to know if locationManager.requestAlwaysAuthorization() has already been asked 我如何知道该应用在后台被杀死后是否已从本地通知中打开? - How can I know if the app has been opened from a local notification after it has been killed in the background? iOS-应用程序运行时不显示推送通知警报 - iOS - Push notification alert is not shown when the app is running 如果其他设备已响应警报,则删除推送通知警报 - Removing Push notification alert if another device has responded to the alert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM