registerForRemoteNotifications() 回调未被调用

Nic*_*ari 6 apple-push-notifications ios9 xcode7

有人问过这个问题(例如这里这里),但似乎没有一个解决方案适用于我的设置。

我正在 iOS 9 / Xcode7 (beta 5) 中开发一个应用程序。

我创建了一个显式的 App ID/包标识符。(应用程序 ID 由 Xcode 自动生成,但在 Developer Portal 中作为 收听Xcode iOS App ID com mycompanyname mayappname)。

我已在 Xcode 中配置了目标的“功能”以添加对推送通知的支持。

我已经在开发者门户中配置了我的 App ID 以支持推送通知:我创建并下载了一个开发 APNs 证书,并安装在我的钥匙串中(不确定这是不是需要?我认为这个证书是为我的服务器准备的?但只是为了如果在构建时需要它是安全的吗?)

我正在设备上从 Xcode 运行应用程序:iPhone 5s、iOS 9、4G 连接、Wi-Fi 关闭(以防万一)。

我的应用程序未列在设备的 Settings.app 的“通知”部分中,因此我无法在那里启用任何内容...

我像这样启动注册过程:

func application(application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?
        ) -> Bool
{
    application.registerForRemoteNotifications()
        
    return true
}
Run Code Online (Sandbox Code Playgroud)

我已经实现了两个:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{

}
Run Code Online (Sandbox Code Playgroud)

...和:

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

并在两者上设置断点/日志,但都没有被调用。

会发生什么?

编辑:它现在正在工作,但前提是我在启动时同时 调用registerForRemoteNotifications() 调用registerUserNotificationSettings()

我的工作假设我应该首先调用registerForRemoteNotifications(),成功后(即application(_:didRegisterForRemoteNotificationsWithDeviceToken))我应该调用registerUserNotificationSettings()指定通知的类型(并提示用户)。文档没有说明必须首先调用这两个函数中的哪一个;特别是,内联文档registerForRemoteNotifications()说:

[...] 调用此方法以启动注册过程[...]

,这似乎表明它本身就触发了整个事情。但是,编程指南有以下分步说明:

  1. 使用 registerUserNotificationSettings: 注册您的应用支持的通知类型。
  2. 通过调用应用程序的 registerForRemoteNotifications 方法注册以通过 APNs 接收推送通知。
  3. 存储服务器返回给应用委托的设备令牌,以便注册成功,或者优雅地处理注册失败。
  4. 将设备令牌转发到应用程序的推送提供程序。

更新(2015 年 1 月):

我转移到另一个项目,所以我没有跟进这个问题,也没有找到明确的回应。还在听任何有用的信息......

Sul*_*ain 3

你很接近。

您需要首先调用registerForRemoteNotificationTypes:registerUserNotificationSettings:(iOS >=8)

然后打电话

registerForRemoteNotifications:
Run Code Online (Sandbox Code Playgroud)

您有该示例代码:

   // Register the supported interaction types.

    UIUserNotificationType types = UIUserNotificationTypeBadge |

                 UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

    UIUserNotificationSettings *mySettings =

                [UIUserNotificationSettings settingsForTypes:types categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];



   // Register for remote notifications.

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

通过: https: //developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html