Ios userNotificationCenter:didReceive:withCompletionHandler太短

Ios userNotificationCenter:didReceive:withCompletionHandler太短,ios,swift,apple-push-notifications,Ios,Swift,Apple Push Notifications,已调用userNotificationCenter:didReceive:withCompletionHandler,但应用程序很快被终止 应用程序已完全终止,我希望在不打开应用程序的情况下响应通知操作。我尝试过使用信号灯,但没有成功 public init() { super.init() self.startBackgroundService() UNUserNotificationCenter.current().delegate

已调用userNotificationCenter:didReceive:withCompletionHandler,但应用程序很快被终止

应用程序已完全终止,我希望在不打开应用程序的情况下响应通知操作。我尝试过使用信号灯,但没有成功

    public init() {
        super.init()
        self.startBackgroundService()

        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self
    }

    public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       didReceive response: UNNotificationResponse,
                                       withCompletionHandler completionHandler: @escaping () -> Void) {
        NSLog("userNotificationCenter:didReceive:withCompletionHandler")
        var data: [String: Any] = ["$": "NotificationResponse"]
        data["notification"] = response.notification.asDictionary()
        data["actionIdentifier"] = response.actionIdentifier
        data["backgroundDispatcher"] = getDispatcherHandle(key: "backgroundDispatcher")

        if let textResponse = response as? UNTextInputNotificationResponse {
            data["$"] = "TextInputNotificationResponse"
            data["userText"] = textResponse.userText
        }

        if (!initialized) {
            // wait for the service to initialize
            semaphore.wait();
        }

        self.background.invokeMethod("notificationResponse", arguments: data) { result in
            completionHandler()
            // the work is done, resume
            self.semaphore.resume();
        }

        // wait for the work to complete
        semaphore.wait();
    }

    private func startBackgroundService() {
        NSLog("startBackgroundService")
        internalQueue.sync {
            if (initialized) {
                return;
            }
        }
        if let info = FlutterCallbackCache.lookupCallbackInformation(getDispatcherHandle(key: "callbackDispatcher")) {
            backgroundRunner.run(withEntrypoint: info.callbackName, libraryURI: info.callbackLibraryPath)
            registrar.addMethodCallDelegate(self, channel: background)
            background.setMethodCallHandler { call, result in
                self.handle(call, result: result)
                // the service is initialized, resume
                self.semaphore.resume();
            }
        }
    }
我也看到了这一点

Apr  9 02:43:19 Razvans-iPhone SpringBoard(UserNotificationsServer)[52] <Notice>: [com.tophap.firebaseNotificationsExample] Application background launch action for notification response action cancel recieved action response <BSActionResponse: 0x280f874a0; error: <NSError: 0x280757060; domain: BSActionErrorDomain; code: 1> {
    description = "The operation couldn\M-b\M^@\M^Yt be completed. (BSActionErrorDomain error 1.)";
}>
Apr  9 02:43:19 Razvans-iPhone SpringBoard(UserNotificationsServer)[52] <Notice>: [com.tophap.firebaseNotificationsExample] Removing process assertion for background notification action
Apr 9 02:43:19 Razvans iPhone SpringBoard(UserNotificationsServer)[52]:[com.tophap.firebaseNotificationsExample]通知响应操作的应用程序后台启动操作取消收到的操作响应
Apr 9 02:43:19 Razvans iPhone SpringBoard(UserNotificationsServer)[52]:[com.tophap.firebaseNotificationsExample]删除后台通知操作的进程断言

在开始任务之前,需要调用
beginBackgroundTask(expirationHandler)
。不需要信号灯。确保在完成所有后台工作之前不要调用提供的
completionHandler
。您可以回答这个问题以便我选择它吗?您需要在开始任务之前调用
beginBackgroundTask(expirationHandler)
。不需要信号灯。确保在完成所有后台工作之前不要调用提供的
completionHandler
,您能回答这个问题以便我选择它吗?