[英]Logging from Notification Service Extension
所以目前我正在這樣做:
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
,那么操作系統將終止擴展,我將沒有時間完成我的日志。
應用程序擴展執行其任務后不久(或啟動后台 session 執行它),系統終止擴展。
Docs: 應用擴展的生命周期
目前它正在工作,但不能保證額外的網絡調用正常工作。
如果日志返回,我只能返回該完成處理程序,但是日志可能會在 60 秒內超時,這本身也是另一個問題。
有沒有人想到任何解決方案?
你在正確的軌道上。
您應該最終從派生自日志記錄的網絡回調的回調中返回contentHandler
。 只需稍作改動:
對於日志的網絡調用,將其URLSessionConfiguration
的timeoutIntervalForRequest
設置為低於 5 秒的值。 這樣,您就不會等待很長時間才能完成。
這是偽代碼,但對於 object 而言,日志記錄會執行以下操作:
if let imageData = data {
self?.log("downloadSuccess") {
completionHandler(imageData, nil)
}
}
它要么在不到 5 秒的時間內成功/失敗,要么如果它將超時,則不會超過 5 秒。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.