iOS PushKit 注册无法在 iOS10 上运行

iOS PushKit registration not working on iOS10

我正在为 iOS 10.

的 VoIP 应用程序实施 PushKit

我在 AppDelegate.swift 文件中写了这段代码:

import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate {

var window: UIWindow?
private let _pushRegistry = PKPushRegistry(queue: DispatchQueue.main)

//MARK: - App Life Cycle -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //Register for local notifications
    NotificationController.registerNotifcationForApplication()
    
    //PushKit
    _pushRegistry.delegate = self
    var typeSet = Set<PKPushType>()
    typeSet.insert(.voIP)
    _pushRegistry.desiredPushTypes = typeSet
    
    window!.makeKeyAndVisible()
    return true
}

//MARK: - Delegates -
func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials,  forType type: PKPushType) {
    DDLogVerbose("PushKit Token: \(credentials.token) length: \(credentials.token.count)")
    print("pushRegistry didUpdate credentials")
}

func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenForType type: PKPushType) {
    DDLogVerbose("")
    print("pushRegistry didInvalidatePushTokenForType")
}

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
    DDLogVerbose("Payload: \(payload.dictionaryPayload)")
    print("pushRegistry didReceiveIncomingPushWith")
}

}

我启用了这些背景模式:

但是当我启动应用程序时,代理的 none 被调用
它也不适用于 iOS 9.

我只是通过启用 Push Notifications 功能解决了这个问题:

其实这个我也试过了,但是以前不行。 现在我启用它,关闭 Xcode 8.2.1,重新打开,清理构建,它成功了!

感谢this SO answer by vin25