ios - iOS 丰富的通知和异步请求 - RxSwift
问题描述
我必须更新批量发送的通知。我必须 :
- 取 Batch 收到的通知内容
- 发送到后端
- 接收更新的内容
- 显示通知。
我的问题是:当我将它发送到后端时,函数 didReceive 完成并且通知在更新之前发送。
如何等待后端的响应?还是你有别的想法?
import BatchExtension
import Core
import Middleware
import RxSwift
class NotificationService: BAENotificationServiceExtension {
var alreadySend: Bool = false
let batchHelper = BAERichNotificationHelper()
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let userInfo = request.content.userInfo
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let defaultMessage = userInfo["on_error"] as? String {
let body = request.content.body
if body.contains("$") {
if let image = userInfo["image"] as? String {
renderPush(title: request.content.title, body: body, image: image, defaultMessage: defaultMessage)
}
}
} else {
super.didReceive(request, withContentHandler: contentHandler)
return
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
private func renderPush(title: String, body: String, image: String?, defaultMessage: String) {
let customPushNotification = CustomPushNotificationContent(body: body, image: image)
let credentialsStore = DummyCredentialStore()
let serviceProvider = ServiceProvider(
networkProvider: NetworkProvider(
endPoint: Server.endPoint,
timeoutIntervalForResource: 10,
timeoutIntervalForRequest: 10
),
credentialsStore: credentialsStore
)
if let credentials = DummyCredentialStore.getCredentialsFromSharedContainer() {
let service = serviceProvider.getPushNotificationService(credentials: credentials)
_ = service.renderPush(pushNotificationContent: customPushNotification)
.subscribe(onNext: { result in
let content = UNMutableNotificationContent()
content.title = title
if result.result != "ok" {
content.body = defaultMessage
} else {
content.body = result.body
}
if result.image != nil {
if let urlImage = URL(string: result.image ?? "") {
do {
let attachement = try UNNotificationAttachment(identifier: "image", url: urlImage, options: nil)
content.attachments.append(attachement)
} catch {}
}
}
self.bestAttemptContent = content
})
}
}
}
解决方案
推荐阅读
- java - 将 .txt 文件中的双精度读入数组列表
- python-3.x - 将字符串中的单个字符转换为大写
- python - Python PyPDF2 编写器不适用于解密
- javascript - 使用 Promise 顺序加载脚本
- c# - uwp 项目无法在 Visual Studio 2017 15.8 上构建
- c - 返回中的 or 运算符?
- react-native - 反应状态变化时如何防止动画滞后
- c# - 在c#中拥有Singleton类的正确方法是什么
- python - 当我尝试以 https://github.com/muatik/naive-bayes-classifier 形式使用 naive-bayes-classifier 时,我遇到了错误
- linux-kernel - 如何使用内核 GPIO 描述符接口