APN在iOS 8.0及更高版本中无法使用UIUserNotificationSettings

时间:2014-10-30 20:21:19

标签: ios iphone ipad ios8 apple-push-notifications

我实际上正在开发适用于iPhone和iPad的应用程序。

我正在实施APN,它适用于iOS< 8.0。在Stackoverflow中寻找我发现了很多关于它的问题以及为iOS 8.0及更高版本实现APN的方法,我只是按照这个步骤进行操作。

我的代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [self registerForRemoteNotification];
  return YES;
}

- (void)registerForRemoteNotification 
{
  if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                        UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  } else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
  }
}

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
   //register to receive notifications
   [application registerForRemoteNotifications];
   NSLog(@"Active notifications: %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
}
#endif

当我检查活动通知时,它返回“UIUserNotificationSettings:0x156c03e0; types:(none);”我不明白为什么,日志仍然显示“试图标记应用程序图标但未获得用户的许可以标记应用程序”。

1 个答案:

答案 0 :(得分:6)

您缺少iOS8的行代码:

[[UIApplication sharedApplication] registerForRemoteNotifications];

像这样:

if ([application respondsToSelector: selector (registerUserNotificationSettings :)]) {
      UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound
                                                                               categories: nil ];
       [[UIApplication sharedApplication] registerUserNotificationSettings: settings];
       [[UIApplication sharedApplication] registerForRemoteNotifications];
   } else {
       [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeAlert |
        UIRemoteNotificationTypeSound];

   }