所以目前我正在这样做:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
logger = AnalyticsManager.shared
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as! UNMutableNotificationContent)
if let notificationContent = bestAttemptContent {
extensionHandler = ServiceExtensionHandler(notificationContent: notificationContent, logger: AnalyticsManager.shared)
extensionHandler?.handleNotificationRequest { modifiedNotificationContent in
guard let modifiedNotificationContent = modifiedNotificationContent else {
contentHandler(notificationContent)
return
}
contentHandler(modifiedNotificationContent)
}
} else {
contentHandler(request.content)
return
}
}
在我的ServiceExtensionHandler
中,如果图像下载成功,我将返回修改后的通知。到目前为止,一切都很好。
但是一旦图像下载成功,我想记录一个事件。问题是,如果我返回contentHandler
,则操作系统将终止该扩展,并且我将没有时间完成日志。
在应用扩展程序执行其任务(或启动一个 后台会话来执行它),系统终止 扩展名。
目前可以正常使用,但不能保证额外的网络通话正常。
我只能在日志返回的情况下返回该completeHandler,但是日志可能会在60秒后超时,这本身也是另一个问题。
有人想到了什么解决方案吗?
您走在正确的轨道上。
您最终应从日志记录的网络回调派生的回调返回contentHandler
。只需稍作更改:
对于日志的网络呼叫,将其URLSessionConfiguration
的timeoutIntervalForRequest
设置为小于5秒。这样,您无需等待很长时间即可完成。
这是伪代码,但是对于执行日志记录的对象,其操作类似于:
timeoutIntervalForRequest
它在不到5秒的时间内成功/失败,或者如果要超时,则不会超过5秒。