ios - APNs 在 iOS 8.0 和更高版本中使用 UIUserNotificationSettings 时不起作用

标签 ios iphone ipad ios8 apple-push-notifications

我实际上正在为 iPhone 和 iPad 开发应用。

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

我的代码是:

- (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);”我不明白为什么,日志仍然显示“正在尝试标记应用程序图标,但尚未收到用户的许可以标记该应用程序”。

最佳答案

您缺少 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];

   }

关于ios - APNs 在 iOS 8.0 和更高版本中使用 UIUserNotificationSettings 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662546/

相关文章:

ios - 如何用 UIGravityBehavior 停止某些事情?

iphone - CoverFlow 用按钮代替图像

iphone - Xcode 在build设置中仅显示有效的代码签名身份

ios - 为什么 UIPageViewController 在翻页后调整其 UIViewControllers View 的大小?

iphone - 如何使用字典中的特定项目创建数组?

c# - 有没有办法使用 XCode 创建 ASP .NET 文件?

ios - 如何在 objective-c 中使用 NSURLConnection 压缩文本文件并发布它

ios - ScrollView 不显示自定义 UIView

iphone - iOS:openURL 可以为零吗?

css - Internet Explorer 中的滚动条 css 样式