NotificationContentProcessor.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // NotificationContentProcessor.swift
  3. // NotificationServiceExtension
  4. //
  5. // Created by huangfeng on 2024/5/29.
  6. // Copyright © 2024 Fin. All rights reserved.
  7. //
  8. import Foundation
  9. @_exported import UserNotifications
  10. enum NotificationContentProcessorItem {
  11. case ciphertext
  12. case level
  13. case badge
  14. case autoCopy
  15. case archive
  16. case setIcon
  17. case setImage
  18. case call
  19. var processor: NotificationContentProcessor {
  20. switch self {
  21. case .ciphertext:
  22. return CiphertextProcessor()
  23. case .level:
  24. return LevelProcessor()
  25. case .badge:
  26. return BadgeProcessor()
  27. case .autoCopy:
  28. return AutoCopyProcessor()
  29. case .archive:
  30. return ArchiveProcessor()
  31. case .setIcon:
  32. return IconProcessor()
  33. case .setImage:
  34. return ImageProcessor()
  35. case .call:
  36. return CallProcessor()
  37. }
  38. }
  39. }
  40. enum NotificationContentProcessorError: Swift.Error {
  41. case error(content: UNMutableNotificationContent)
  42. }
  43. public protocol NotificationContentProcessor {
  44. /// 处理 UNMutableNotificationContent
  45. /// - Parameters:
  46. /// - identifier: request.identifier, 有些 Processor 需要,例如 CallProcessor 需要这个去添加 LocalNotification
  47. /// - bestAttemptContent: 需要处理的 UNMutableNotificationContent
  48. /// - Returns: 处理成功后的 UNMutableNotificationContent
  49. /// - Throws: 处理失败后,应该中断处理
  50. func process(identifier: String, content bestAttemptContent: UNMutableNotificationContent) async throws -> UNMutableNotificationContent
  51. /// serviceExtension 即将终止,不管 processor 是否处理完成,最好立即调用 contentHandler 交付已完成的部分,否则会原样展示服务器传递过来的推送
  52. func serviceExtensionTimeWillExpire(contentHandler: (UNNotificationContent) -> Void)
  53. }
  54. extension NotificationContentProcessor {
  55. func serviceExtensionTimeWillExpire(contentHandler: (UNNotificationContent) -> Void) {}
  56. }