【问题标题】:Cannot Get Permission Prompt无法获得许可提示
【发布时间】:2026-02-15 09:35:01
【问题描述】:

解决方法在下面作为答案发布,我得出的结论是这是一个 iOS 8-8.1+ 错误,据我所知尚未修复。

原帖:

我不知所措,这之前可以工作,但在对我的应用程序进行多次更改和更新后它不再工作,我不能 100% 确定这是由于 iOS 8 还是我的应用程序,因为这个应用程序已经很长一段时间的 WIP。

如果我(用户)想要允许来自我的应用的通知,我不会收到提示。

更新:我在另一台设备(iPhone 5、iOS 8.1.2)上测试了我的应用程序并成功请求许可,但它仍然无法在我的设备(iPhone 6+、iOS 8.0)上运行。

(为清楚起见进行编辑:该测试是该应用程序在 iPhone 5、iOS 8.1.2 上的首次安装,并且如更新 2 所述,在 iPhone 5 上重新安装时问题仍然存在。)

更新 2:当我从第二台设备(更新 1:iPhone 5)卸载该应用程序时,它再也没有提示我获得通知权限,但它仍然有效,并且日志还显示设置的权限。但它仍然提示用户提供位置权限。

错误:

2014-12-17 09:49:51.852 Attempting to schedule a local notification  <UIConcreteLocalNotification: 0x170344620>{fire date = (null), time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, December 17, 2014 at 9:49:51 AM Central Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts

2014-12-17 09:49:51.853 Attempting to schedule a local notification <UIConcreteLocalNotification: 0x170344620>{fire date = (null), time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, December 17, 2014 at 9:49:51 AM Central Standard Time, user info = (null)} with a sound but haven't received permission from the user to play sounds

我以前可以正常工作的代码:

AppDelegate : didFinishLaunchingWithOptions

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

我已经尝试过的代码:

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

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

&

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

&

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
    [application registerUserNotificationSettings:settings];

    NSLog(@"current notifications : %@", [application currentUserNotificationSettings]);

    [application registerForRemoteNotifications];
}
else
{
    [application registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

我已经在 AppDelegate : didFinishLaunchingWithOptions 和 IBAction 上的 FirstViewController 中单独和一起尝试了所有这些(当然用 [UIApplication sharedApplication] 替换应用程序)

在我尝试设置权限后,我尝试签入 NSLog:

NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);

NSLog:

current notifications : <UIUserNotificationSettings: 0x170435ee0; types: (none);>

在我的应用程序中,我请求允许使用他们的位置就好了,并尝试在询问他们的位置之前和之后请求通知权限,但这没有任何区别。

位置许可代码可以正常工作..

self.locationManager = [[CLLocationManager alloc] init];
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

【问题讨论】:

  • 听起来你的意思是这是 iOS 8 中的一个错误,在 iOS 8.1 中已修复。这就是它不起作用的地方和它起作用的地方之间的区别,对吧?
  • 我不认为这是权限工作与否之间的错误,而是用户安装应用后提示权限未清除的提示。基本上使它永久用于应用程序。
  • 没错。但在 iOS 8.1 中这是固定的吗?
  • 不,不是。在任何 iOS 8(8.0 或 8.1)上,用户删除并重新安装应用程序后,它都不会提示通知权限。
  • 当开发人员允许应用程序在他们的第一次测试中获得通知时可能会被忽视,除非它完全是一个新设备,否则他们将永远不会再次收到提示,所以在我的情况下,我必须说不允许一开始的通知,现在我无法重新提示。

标签: ios objective-c uilocalnotification


【解决方案1】:

应用程序应始终检查权限状态:

let settings = UIApplication.sharedApplication().currentUserNotificationSettings()

当应用程序意识到我们没有权限时,它应该发出警报以用户直接带到设置应用程序中的正确位置。如果用户接受,则运行此代码(这是 Swift 但我相信您可以翻译):

let url = NSURL(string:UIApplicationOpenSettingsURLString)!
UIApplication.sharedApplication().openURL(url)

这是 iOS 8 的一项新功能。

【讨论】:

    【解决方案2】:

    在对许多设备进行多次测试后,我得出结论,iOS 8 本身存在此问题。经过我的测试,我没有注意到这个问题在 8.0 或 8.1 中得到了修复。我还没有测试所有版本,所以不知道这个错误是否仍然存在。我找到了一种解决方法,说明如下。

    解决方法:

    用户(也就是您)可以解决的问题是不提示显示 UILocalNotifications 的权限是允许应用手动显示通知。

    注意:* 每次卸载并重新安装应用程序时,您都需要重复此操作。 *

    转到:

    Settings > Notifications
    

    然后在列表中找到您的应用并选择它。他们将设置应用程序允许和不允许对通知进行的操作。确保切换“允许通知”处于打开状态,并且其他设置根据需要。

    对开发者的警告:

    当开发人员允许应用在您的第一次测试中获取通知时,此错误可能会被忽视,除非它完全是新设备,否则您将永远不会再次收到提示,因此如果开发人员在苹果提示时不允许通知一开始你不能得到重新提示。无论您批准还是拒绝,同样的错误仍然适用,您只会在设备上收到一次提示,删除并重新安装不会再次提示您。

    在我的情况下:

    在我的测试过程中,我必须按下不允许,以获得通知权限,并且由于 iOS 8 中的错误,它一直卡在永久状态,不会再次提示我获得许可。然后我转到“设置”>“通知”并注意到我的应用列在“不包含”下,然后我从设置中选择了我的应用并启用了它们。

    【讨论】: