查看了有关此问题的过去问题,但这些问题的答案已应用于我的项目,但 pushRegistry() 委托方法没有被调用。
首先是代码:
import UIKit
import PushKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
UNUserNotificationCenter.current().delegate = self
UIApplication.shared.registerForRemoteNotifications()
let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
voipRegistry.delegate = self;
return true
}
...
}
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
NSLog("voip token: \(pushCredentials.token)")
}
已启用以下应用程序功能: - 推送通知 - 后台模式 - 后台获取 - 远程通知
我正在使用 Xcode 9.1 Beta,它不再具有 Voip 作为显式功能,显然这不再需要并且导入 PushKit 就足够了。
在供应门户上,已为应用程序 ID 启用推送通知。(已经生成了一个无效推送证书,但显然还不能使用它来发送推送)