无法注册推送通知(Xcode 7,iOS9)

use*_*999 3 push-notification apple-push-notifications ios9 xcode7

我已将设备升级到iOS 9,将Xcode环境升级到7.0(7A220).我的app通过以下方式注册通知:

[[UIApplication sharedApplication] registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

但是,既没有调用"didRegisterForRemoteNotificationWithDeviceToken"或"didFailToRegisterForRemoteNotificationsWithError".此外,我的应用程序没有出现在设置 - >通知部分(它告诉我它甚至没有尝试注册远程/推送通知)

我的应用程序ID启用了以下应用程序服务:

  • 游戏中心
  • 在应用程序内购买
  • 推送通知

在Xcode中,启用了以下功能:

  • 推送通知

  • 后台模式(远程通知)

这适用于iOS 8,应用程序使用Xcode 6构建.此外,当使用Xcode 7构建时,它不再适用于iOS 8设备.

小智 14

你使用模拟器吗?

在模拟器中,不支持远程通知.

示例代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let pushSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge ,UIUserNotificationType.Sound ,UIUserNotificationType.Alert], categories: nil)
    application.registerUserNotificationSettings(pushSettings)
    return true
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    application.registerForRemoteNotifications()
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let token=deviceToken.description
    print(token)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

Xcode说:

Error Domain=NSCocoaErrorDomain
Code=3010 "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION"
UserInfo={NSLocalizedDescription=REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}
Run Code Online (Sandbox Code Playgroud)


use*_*999 8

它不工作的原因是因为我没有打电话:

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
Run Code Online (Sandbox Code Playgroud)

致电之前:

[[UIApplication sharedApplication] registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)