UIUserNotificationSettings 警报导致应用程序退出 Activity

UIUserNotificationSettings Alert Causes App to Resign Activity

我目前有一个需要本地通知的应用程序,所以我自然必须询问用户是否愿意 'Allow Notifications'。这是我这样做的方法:

AppDelegate.m

// Local Notifications
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
    }

在应用程序初始加载时,我有一个 AVAudioPlayer 开始播放。我已将其设置为当用户离开应用程序(应用程序进入后台)时音乐会淡出并暂停。

问题是在第一次启动应用程序时,当弹出通知要求用户允许通知时,应用程序似乎是被愚弄以为它即将辞职activity。我似乎无法弄清楚如何避免 JUST 通知警报触发此事件,或者在最坏的情况下 "bring the app back",并在警报被以下之一解除后变得活跃2 个通知选项。

这是我在 ViewController 中调用的内容,用于在应用程序更改状态时通知它:

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    ...

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseAudio)
                                                 name:UIApplicationWillResignActiveNotification object:nil];
    ...
}

如果有人能告诉我如何忽略通知警报,使其不认为它会辞职 activity、如何在之后恢复应用程序它 "resign"。我目前有这些其他通知句柄试图将其恢复,但在警报 "messes everything up":

之后不会调用它们

(在同一个ViewControllerviewDidLoad)

...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(animateLoginScreen)
                                                 name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeAudio)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];

感谢您对此提供的任何帮助。谢谢!

每当显示 "system dialog"(例如通知权限对话框)时,您的应用程序 确实 退出活动状态,因为它是 iOS,而不是您的应用程序,负责处理权限请求。

您的应用不是 'fooled',它确实在显示对话框的时间段内退出活动状态。我怀疑你应该在你的应用程序进入后台时暂停你的音频,而不仅仅是在它退出活动时。 UIApplicationDidBecomeActiveNotification 如果您收到 willResignActive,则应该发布。您是否调用了相应的 AppDelegate 方法?