pushRegistry(:didReceiveIncomingPushWith) 在应用程序处于后台时未触发

问题描述

我正在使用 Push Kit 和 Call Kit 开发语音聊天应用。

问题:使用 Storyboard 初始化委托 ViewController 时未触发推送通知

如果 PushKit 委托是从 rootViewController 设置的,则每当应用收到语音调用时,此 pushRegistry 方法都会正确触发,但如果委托设置为从 Storyboard 初始化的 ViewController,则当应用处于后台时,该方法不会触发.

func pushRegistry(_ registry: PKPushRegistry,didReceiveIncomingPushWith payload: PKPushPayload,for type: PKPushType,completion: @escaping () -> Void) {
}

因此,当我的 CallViewController 在 AppDelegate 中像这样初始化时,我的应用程序可以接收 voip 推送通知

protocol PushKitEventDelegate: AnyObject {
    func credentialsUpdated(credentials: PKPushCredentials) -> Void
    func credentialsInvalidated() -> Void
    func incomingPushReceived(payload: PKPushPayload) -> Void
    func incomingPushReceived(payload: PKPushPayload,completion: @escaping () -> Void) -> Void
}

@main
class AppDelegate: UIResponder,UIApplicationDelegate,PKPushRegistryDelegate {

    var pushKitEventDelegate: PushKitEventDelegate?
    var voipRegistry = PKPushRegistry.init(queue: dispatchQueue.main)
    var callViewController: CallViewController?

    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // This way of getting CallViewController works
        callController = UIApplication.shared.windows.first?.rootViewController as! CallViewController

        self.pushKitEventDelegate = callViewController

但是当 CallViewController 像这样初始化时,我的应用程序无法收到通知

    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // This way of getting CallViewController does not work
        let storyboard = UIStoryboard(name: "Main",bundle: nil)
        callViewController = storyboard.instantiateViewController(identifier: "callVC") as! CallViewController

        self.pushKitEventDelegate = callViewController

使用 Storyboard 在第二种方法中初始化 CallViewController 时,如果应用程序处于后台模式或终止,应用程序将无法收到 voip 通知

从 Storyboard 初始化 ViewController 有什么问题吗?我无法将 CallViewController 设置为 rootViewController,因此我想从 Storyboard 对其进行初始化。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)