UIUserNotificationSettings类型返回错误的值

时间:2015-01-23 06:18:57

标签: ios objective-c push-notification

我的情况如下:

  1. 安装我的应用
  2. 转到设备的应用设置并关闭我的应用的推送通知。
  3. 删除我的应用
  4. 重新安装我的应用并以编程方式获取推送通知设置,如下所示:

    if ([[UIApplication sharedApplication]respondsToSelector:@selector(currentUserNotificationSettings)]){
            UIUserNotificationSettings *noticationSettings =     [[UIApplication sharedApplication] currentUserNotificationSettings];
               if (!noticationSettings || (noticationSettings.types ==   UIUserNotificationTypeNone)) {
                 return NO;
           }
        return YES;
    }
    
  5. 但是,notificationSettings.types始终返回UIUserNotificationTypeNone,但在设备的应用设置中,通知设置正在启用。 在这种情况下,有人可以帮助我获得正确的通知设置值吗?

2 个答案:

答案 0 :(得分:2)

你应该检查

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
致电前

[[UIApplication sharedApplication] currentUserNotificationSettings];

这是正确的代码:

    if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications] == NO)
        return NO;
if ([[UIApplication sharedApplication]respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *noticationSettings =     [[UIApplication sharedApplication] currentUserNotificationSettings];
           if (!noticationSettings || (noticationSettings.types ==   UIUserNotificationTypeNone)) {
             return NO;
       }
    return YES;
}

答案 1 :(得分:1)

似乎在打电话 [[UIApplication sharedApplication] currentUserNotificationSettings] 在打电话之前 [[UIApplication sharedApplication] registerUserNotificationSettings:]返回错误的值。