首页 > 解决方案 > iOS 丰富的通知和异步请求 - RxSwift

问题描述

我必须更新批量发送的通知。我必须 :

  1. 取 Batch 收到的通知内容
  2. 发送到后端
  3. 接收更新的内容
  4. 显示通知。

我的问题是:当我将它发送到后端时,函数 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
            })
    }
}

}

标签: iosswiftpush-notificationrx-swift

解决方案


推荐阅读