Ios 您可以在通知服务中运行后台任务吗?

Ios 您可以在通知服务中运行后台任务吗?,ios,swift,swift3,ios10,Ios,Swift,Swift3,Ios10,我有一个推送通知服务(UNNotificationServiceExtension),它拦截我的推送通知以显示丰富的推送通知。我试图联系我的服务器,但我认为服务在运行所有代码之前就被终止了。我通过结尾处的[modified]位发现了这一点,没有我的代码,也没有我的代码 我已经查看了静默通知,但我发现当用户杀死应用程序时,它不会运行,所以这是不好的 现在我想做一些事情,大致如下: let queue = DispatchQueue.global(qos: .background) queue.a

我有一个推送通知服务(UNNotificationServiceExtension),它拦截我的推送通知以显示丰富的推送通知。我试图联系我的服务器,但我认为服务在运行所有代码之前就被终止了。我通过结尾处的[modified]位发现了这一点,没有我的代码,也没有我的代码

我已经查看了静默通知,但我发现当用户杀死应用程序时,它不会运行,所以这是不好的

现在我想做一些事情,大致如下:

let queue = DispatchQueue.global(qos: .background) 
queue.async {
    self.contactServer()
}
它运行,但从未联系过服务器。所以现在我不确定代码是否有效?我该怎么办?我如何将任务传递给系统,以便在后台运行它,因为我认为该服务太快,无法真正等待联系服务器。对不起,我对swift和iOS都是新手

有没有一种方法可以让我做一些事情:

await contactServer() 
注意:我知道斯威夫特没有等待,但你明白我想说的

这是我的全部代码:

import UserNotifications
import ServerEngine
import UIKit

class NotificationService: UNNotificationServiceExtension {


var backgroundTask: UIBackgroundTaskIdentifier!
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

    if let bestAttemptContent = bestAttemptContent {
        // Modify the notification content here...
        bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

        doBgTask()


        contentHandler(bestAttemptContent)
    }
}


func doBgTask() {
    let queue = DispatchQueue.global(qos: .background)
    queue.async {
        serverEngine.touchBase()
    }
}

你解决了这个问题吗?不,很遗憾,我没有。但我确实发现,您可以从服务中通过简单的http调用访问服务器。苹果在其功能上留下了空白,让我们来解决它。对不起:(