xcode isRegisteredForRemoteNotifications 总是返回 no

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28242332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:34:28  来源:igfitidea点击:

isRegisteredForRemoteNotifications always returns no

objective-ciphonexcodeios8apple-push-notifications

提问by marshy101

I got the pop up, i have accepted, i see it in the notifications and it is turned on but this code always returns no and i cant seem to find out why

我收到了弹出窗口,我已接受,我在通知中看到它并且它已打开,但此代码始终返回 no,我似乎无法找出原因

UIApplication *application = [UIApplication sharedApplication];

BOOL enabled;

// Try to use the newer isRegisteredForRemoteNotifications otherwise use the enabledRemoteNotificationTypes.
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{

    enabled = [application isRegisteredForRemoteNotifications];
}
else
{
    UIRemoteNotificationType types = [application enabledRemoteNotificationTypes];
    enabled = types & UIRemoteNotificationTypeAlert;

}
if  (enabled){
    NSLog(@"is registered");
}else{
     NSLog(@"is not registered");
}

回答by yingshanliu

I think what makes it happen might be:

我认为导致它发生的原因可能是:

  1. isRegisteredForRemoteNotifications will always return NO in simulators.
  2. registerForRemoteNotifications fails.
  1. isRegisteredForRemoteNotifications 将始终在模拟器中返回 NO。
  2. registerForRemoteNotifications 失败。

回答by luisfcofv

I was struggling with the same problem, this worked for me.

我正在努力解决同样的问题,这对我有用。

BOOL enabled = NO;
UIUserNotificationSettings *notificationSettings = [SharedApplication currentUserNotificationSettings];
enabled = notificationSettings.types < 4;

回答by Popeye

As per the Apple documentation isRegisteredForRemoteNotificationswill return NOif registration has not occurred, has failed, or has been denied by the user. YESwill be returned if the app has registered for remote notifications and has received is device token. So in answer to your question NOit will not always return noit will also return yesif a your app has registered for remote notifications and it has received it device token.

如果注册未发生、失败或被用户拒绝,Apple 文档isRegisteredForRemoteNotifications将返回NOYES如果应用程序已注册远程通知并收到设备令牌,则将返回。因此,在回答您的问题NO它不会总是返回no它也会返回yes如果您的应用程序已注册远程通知并且它已收到它的设备令牌。

Checkout the Apple Documentation for a better description

查看 Apple 文档以获得更好的描述

Return Value

YESif the app is registered for remote notifications and received its device token or NOif registration has not occurred, has failed, or has been denied by the user.

Discussion

This method reflects only the successful completion of the remote registration process that begins when you call the registerForRemoteNotificationsmethod. This method does not reflect whether remote notifications are actually available due to connectivity issues. The value returned by this method takes into account the user's preferences for receiving remote notifications.

返回值

YES如果应用程序注册了远程通知并收到了它的设备令牌,或者NO注册没有发生、失败或被用户拒绝。

讨论

此方法仅反映在您调用该registerForRemoteNotifications方法时开始的远程注册过程的成功完成。由于连接问题,此方法不反映远程通知是否实际可用。此方法返回的值考虑了用户接收远程通知的偏好。

回答by Shaheen Ghiassy

I think the simulator will always return no, try running your code on a real device and see if the behavior continues

我认为模拟器总会返回no,尝试在真实设备上运行您的代码,看看行为是否继续

回答by Mig70

After iOS10 and to make it work both on the simulator and on a real device you should use something like this:

在 iOS10 之后,为了让它在模拟器和真实设备上都能工作,你应该使用这样的东西:

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
     if (settings.alertStyle == UNAlertStyleNone)
         NSLog(@“ACCESS DENIED!”);
     else
         NSLog(@“ACCESS GRANTED!”);
 }];

If you are NOT planning to test on the simulator you may use the code below (unfortunately this will always return NO on the simulator):

如果您不打算在模拟器上进行测试,您可以使用下面的代码(不幸的是,这将始终在模拟器上返回 NO):

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

Do NOT use the code below if you are planning to compile for iOS 10 or iOS11 because it has been deprecated on iOS10:

如果您计划为 iOS 10 或 iOS11 编译,请不要使用下面的代码,因为它已在 iOS10 上被弃用:

[SharedApplication currentUserNotificationSettings];