从 Notification Service Extension 登录
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
如果图片下载成功,我会 return 修改通知。到目前为止,一切都很好。
但是一旦图像下载成功,我想记录一个事件。
问题是如果我 return contentHandler
那么 OS 将终止扩展,我将没有时间完成我的日志。
Shortly after the app extension performs its task (or starts a
background session to perform it), the system terminates the
extension.
目前它正在运行,但不保证额外的网络调用一定会运行。
如果日志 returns,我只能 return 那个 completionHandler,但是日志可能会在 60 秒内超时,这本身也是另一个问题。
有没有人想过解决办法?
你走在正确的轨道上。
您应该 return contentHandler
最终来自从日志记录的网络回调派生的回调。只是做了一个小改动:
对于您日志的网络调用,将其 URLSessionConfiguration
s timeoutIntervalForRequest
设置为低于 5 秒的值。这样您就不必等待它完成。
这是伪代码,但对于执行日志记录的对象,请执行以下操作:
if let imageData = data {
self?.log("downloadSuccess") {
completionHandler(imageData, nil)
}
}
它要么 succeeds/fails 不到 5 秒,要么如果超时,则不会超过 5 秒。
所以目前我正在这样做:
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
如果图片下载成功,我会 return 修改通知。到目前为止,一切都很好。
但是一旦图像下载成功,我想记录一个事件。
问题是如果我 return contentHandler
那么 OS 将终止扩展,我将没有时间完成我的日志。
Shortly after the app extension performs its task (or starts a background session to perform it), the system terminates the extension.
目前它正在运行,但不保证额外的网络调用一定会运行。
如果日志 returns,我只能 return 那个 completionHandler,但是日志可能会在 60 秒内超时,这本身也是另一个问题。
有没有人想过解决办法?
你走在正确的轨道上。
您应该 return contentHandler
最终来自从日志记录的网络回调派生的回调。只是做了一个小改动:
对于您日志的网络调用,将其 URLSessionConfiguration
s timeoutIntervalForRequest
设置为低于 5 秒的值。这样您就不必等待它完成。
这是伪代码,但对于执行日志记录的对象,请执行以下操作:
if let imageData = data {
self?.log("downloadSuccess") {
completionHandler(imageData, nil)
}
}
它要么 succeeds/fails 不到 5 秒,要么如果超时,则不会超过 5 秒。