|
| 1 | +// |
| 2 | +// NotificationService.swift |
| 3 | +// NotificationServiceExtension |
| 4 | +// |
| 5 | +// Created by Naman Jain on 10/09/19. |
| 6 | +// Copyright © 2019 Naman Jain. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UserNotifications |
| 10 | + |
| 11 | +class NotificationService: UNNotificationServiceExtension { |
| 12 | + |
| 13 | + var contentHandler: ((UNNotificationContent) -> Void)? |
| 14 | + var bestAttemptContent: UNMutableNotificationContent? |
| 15 | + |
| 16 | + override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { |
| 17 | + self.contentHandler = contentHandler |
| 18 | + bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) |
| 19 | + |
| 20 | + guard let bestAttemptContent = bestAttemptContent, |
| 21 | + let attURLStringDict = bestAttemptContent.userInfo["fcm_options"] as? [String: String], |
| 22 | + let attURLString = attURLStringDict["image"], |
| 23 | + let attURL = URL(string: attURLString) else { |
| 24 | + print("Failed to get Image Url") |
| 25 | + return |
| 26 | + } |
| 27 | + request.content.setValue("hello", forKey: "categoryIdentifier") |
| 28 | + |
| 29 | + print(request.content.categoryIdentifier) |
| 30 | + |
| 31 | + downloadImageFrom(url: attURL) { (attachment) in |
| 32 | + if let attachment = attachment { |
| 33 | + bestAttemptContent.attachments = [attachment] |
| 34 | + contentHandler(bestAttemptContent) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + override func serviceExtensionTimeWillExpire() { |
| 41 | + // Called just before the extension will be terminated by the system. |
| 42 | + // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. |
| 43 | + if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { |
| 44 | + contentHandler(bestAttemptContent) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void){ |
| 49 | + let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in |
| 50 | + guard let downloadedUrl = downloadedUrl else { |
| 51 | + completionHandler(nil) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + var urlPath = URL(fileURLWithPath: NSTemporaryDirectory()) |
| 56 | + let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".jpg" |
| 57 | + urlPath = urlPath.appendingPathComponent(uniqueURLEnding) |
| 58 | + |
| 59 | + try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath) |
| 60 | + |
| 61 | + do{ |
| 62 | + let attachment = try UNNotificationAttachment(identifier: "picture", url: urlPath, options: nil) |
| 63 | + completionHandler(attachment) |
| 64 | + } |
| 65 | + catch{ |
| 66 | + completionHandler(nil) |
| 67 | + } |
| 68 | + } |
| 69 | + task.resume() |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | + |
0 commit comments