如何处理用户选择通知设置权限警报

How to handle user choice of notification settings permission alert

在某些视图控制器中,我弹出此通知设置消息:

My app would like to to send you Notifications which may include alerts, sounds and icon badges. These can be configured in Settings

如何处理消息的 Don't AllowAllow 按钮?

如果用户点击 Allow,我需要在同一视图控制器中更改标签文本。

我假设您的应用中某处有这样的代码:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:... categories:...];
[application registerUserNotificationSettings:settings];

这样做之后,系统将显示您正在谈论的警报。在那一刻,应用程序将收到 UIApplicationWillResignActiveNotification(您可以在应用程序委托中或通过 NSNotificationCenter 注册)。然后,在用户做出选择后,系统将发送 UIApplicationDidBecomeActiveNotification(也可以在应用程序委托中或通过通知)。那时,使用如下代码检查权限:

UIUserNotificationSettings *settings = application.currentUserNotificationSettings;
if (settings.types & UIUserNotificationTypeSound & UIUserNotificationTypeBadge) {
    // sound and icon badge allowed
}
else {
    // either sound or icon badge or both disallowed
}

以下答案已解决问题。

当系统级弹出消息可见时,应用程序将调用 resignActivity。当您单击 "Allow""Don't Allow" 警报按钮时,您的警报消息将被取消并调用 applicationDidBecomeActive 在你的 AppDelegate class 中注册 NSNotificationCenter 可以在用户即时时更改文本。

- (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_KEY"
                                                            object:self];
    });
}