12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // NotificationService.swift
- // NotificationService
- //
- // Created by 南鑫林 on 2019/9/10.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UserNotifications
- import UIKit
- class NotificationService: UNNotificationServiceExtension {
- var contentHandler: ((UNNotificationContent) -> Void)?
- var bestAttemptContent: UNMutableNotificationContent?
- override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
- self.contentHandler = contentHandler
- //[modified]这个是一个标示,可以实现对服务器下发下来的内容进行更改
- bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
-
- func failEarly() {
- contentHandler(request.content)
- }
- if let bestAttemptContent = bestAttemptContent {
- let apsDic = request.content.userInfo["aps"] as? [AnyHashable : Any]
- let attachUrl = apsDic?["image"] as? String
- let category = apsDic?["category"] as? String
- bestAttemptContent.categoryIdentifier = category ?? ""
- let url = URL(string: attachUrl ?? "")
- guard let imageData = NSData(contentsOf:url!) else { return failEarly() }
-
- let image = UIImage(data: imageData as Data)
- let identifier = ProcessInfo.processInfo.globallyUniqueString
- if let attachment = UNNotificationAttachment.create(identifier: identifier, image: image!, options: nil) {
- // where myImage is any UIImage that follows the
- bestAttemptContent.attachments = [attachment]
- }
- contentHandler(bestAttemptContent)
- }
- }
-
- override func serviceExtensionTimeWillExpire() {
- // Called just before the extension will be terminated by the system.
- // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
- if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
- contentHandler(bestAttemptContent)
- }
- }
- }
- extension UNNotificationAttachment {
-
- static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
- let fileManager = FileManager.default
- let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
- let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
- do {
- try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
- let imageFileIdentifier = identifier+".png"
- let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
- guard let imageData = image.pngData() else {
- return nil
- }
- try imageData.write(to: fileURL)
- let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
- return imageAttachment
- } catch {
- print("error " + error.localizedDescription)
- }
- return nil
- }
-
- /// Save the image to disk
- static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
- let fileManager = FileManager.default
- let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
- let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
-
- do {
- try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
- let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
- try data.write(to: fileURL!, options: [])
- let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL!, options: options)
- return imageAttachment
- } catch let error {
- print("error \(error)")
- }
-
- return nil
- }
- }
|