summaryrefslogtreecommitdiff
path: root/JuickPush/NotificationService.swift
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-09-16 17:39:47 +0300
committerGravatar Vitaly Takmazov2020-12-10 18:59:55 +0300
commit77cd2d89c49285f02efabb42c46a4f81e61d5e6f (patch)
tree8f11e91d4c60ec3d493d6b64e8f35bf8985e9091 /JuickPush/NotificationService.swift
parentaa6e2444af3bb07180550b646ad8f0d3ca78ddfb (diff)
NotificationService rewritten in Swift, added dummy test target
Diffstat (limited to 'JuickPush/NotificationService.swift')
-rw-r--r--JuickPush/NotificationService.swift48
1 files changed, 48 insertions, 0 deletions
diff --git a/JuickPush/NotificationService.swift b/JuickPush/NotificationService.swift
new file mode 100644
index 0000000..380d2b0
--- /dev/null
+++ b/JuickPush/NotificationService.swift
@@ -0,0 +1,48 @@
+//
+// NotificationService.swift
+// JuickPush
+//
+// Created by Vitaly Takmazov on 16.09.2020.
+// Copyright © 2020 com.juick. All rights reserved.
+//
+
+import Foundation
+import UserNotifications
+
+class NotificationService : UNNotificationServiceExtension {
+ var contentHandler: ((UNNotificationContent) -> Void)?
+ var bestAttemptContent: UNMutableNotificationContent?
+ override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
+ self.contentHandler = contentHandler;
+ self.bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent
+ if let bestAttemptContent = self.bestAttemptContent {
+ if let avatarURL = URL(string: (request.content.userInfo["avatarUrl"] as? String)!) {
+ let task = URLSession.shared.downloadTask(with: avatarURL) {
+ (location, response, error) in
+ if error == nil {
+ if let suggestedName = response?.suggestedFilename {
+ let temporaryDirectory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(suggestedName)
+ if let currentPath = location?.path {
+ do {
+ try FileManager.default.moveItem(atPath: currentPath, toPath: temporaryDirectory.path)
+ if let avatarAttachment = try? UNNotificationAttachment(identifier: avatarURL.lastPathComponent, url: temporaryDirectory, options: nil) {
+ bestAttemptContent.attachments = [avatarAttachment]
+ self.contentHandler?(bestAttemptContent)
+ }
+ } catch {
+ self.contentHandler?(bestAttemptContent)
+ }
+ }
+ }
+ }
+ }
+ task.resume()
+ }
+ }
+ }
+ override func serviceExtensionTimeWillExpire() {
+ if let bestAttemptContent = bestAttemptContent, let contentHandler = contentHandler {
+ contentHandler(bestAttemptContent)
+ }
+ }
+}