userNotificationCenter:didReceive:withCompletionHandler太短

时间:2019-04-08 23:39:27

标签: ios swift apple-push-notifications

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

0 个答案:

没有答案