NotificationService.swift 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NotificationService.swift
  3. // NotificationService
  4. //
  5. // Created by 南鑫林 on 2019/9/10.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UserNotifications
  9. import UIKit
  10. class NotificationService: UNNotificationServiceExtension {
  11. var contentHandler: ((UNNotificationContent) -> Void)?
  12. var bestAttemptContent: UNMutableNotificationContent?
  13. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  14. self.contentHandler = contentHandler
  15. //[modified]这个是一个标示,可以实现对服务器下发下来的内容进行更改
  16. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  17. func failEarly() {
  18. contentHandler(request.content)
  19. }
  20. if let bestAttemptContent = bestAttemptContent {
  21. let apsDic = request.content.userInfo["aps"] as? [AnyHashable : Any]
  22. let attachUrl = apsDic?["image"] as? String
  23. let category = apsDic?["category"] as? String
  24. bestAttemptContent.categoryIdentifier = category ?? ""
  25. let url = URL(string: attachUrl ?? "")
  26. guard let imageData = NSData(contentsOf:url!) else { return failEarly() }
  27. let image = UIImage(data: imageData as Data)
  28. let identifier = ProcessInfo.processInfo.globallyUniqueString
  29. if let attachment = UNNotificationAttachment.create(identifier: identifier, image: image!, options: nil) {
  30. // where myImage is any UIImage that follows the
  31. bestAttemptContent.attachments = [attachment]
  32. }
  33. contentHandler(bestAttemptContent)
  34. }
  35. }
  36. override func serviceExtensionTimeWillExpire() {
  37. // Called just before the extension will be terminated by the system.
  38. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  39. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  40. contentHandler(bestAttemptContent)
  41. }
  42. }
  43. }
  44. extension UNNotificationAttachment {
  45. static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
  46. let fileManager = FileManager.default
  47. let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
  48. let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
  49. do {
  50. try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
  51. let imageFileIdentifier = identifier+".png"
  52. let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
  53. guard let imageData = image.pngData() else {
  54. return nil
  55. }
  56. try imageData.write(to: fileURL)
  57. let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
  58. return imageAttachment
  59. } catch {
  60. print("error " + error.localizedDescription)
  61. }
  62. return nil
  63. }
  64. /// Save the image to disk
  65. static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
  66. let fileManager = FileManager.default
  67. let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
  68. let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
  69. do {
  70. try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
  71. let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
  72. try data.write(to: fileURL!, options: [])
  73. let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL!, options: options)
  74. return imageAttachment
  75. } catch let error {
  76. print("error \(error)")
  77. }
  78. return nil
  79. }
  80. }