2

我正在为 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")
}

}

我启用了这些背景模式: 在此处输入图像描述

但是当我启动应用程序时,没有一个代表被调用。
它也不适用于 iOS 9。

4

1 回答 1

4

我只是通过启用Push Notifications以下功能来解决它​​:

在此处输入图像描述

其实我也试过了,但以前没有用过。现在我启用它,关闭 Xcode 8.2.1,重新打开,清理构建,它工作了!

感谢vin25的这个SO回答

于 2017-01-10T15:51:59.647 回答