无法获取权限提示



解决方法作为答案发布在下面,我得出结论,这是一个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:当我从第二台设备(Update1: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];
}

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

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

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

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

这是iOS 8的一项新功能。

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

解决方法:

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

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

转到:

Settings > Notifications

然后在列表中找到您的应用程序并选择它。它们将是允许和不允许应用对通知执行的操作的设置。确保切换开关"允许通知"已打开,并且其他设置符合要求。

对开发人员的警告:

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

在我的情况下:

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

相关内容

最新更新