diff options
Diffstat (limited to 'JuickPush')
-rw-r--r-- | JuickPush/Info.plist | 2 | ||||
-rw-r--r-- | JuickPush/JuickPush-Bridging-Header.h | 4 | ||||
-rw-r--r-- | JuickPush/NotificationService.h | 13 | ||||
-rw-r--r-- | JuickPush/NotificationService.m | 46 | ||||
-rw-r--r-- | JuickPush/NotificationService.swift | 48 |
5 files changed, 53 insertions, 60 deletions
diff --git a/JuickPush/Info.plist b/JuickPush/Info.plist index 9aeee3f..721973c 100644 --- a/JuickPush/Info.plist +++ b/JuickPush/Info.plist @@ -25,7 +25,7 @@ <key>NSExtensionPointIdentifier</key> <string>com.apple.usernotifications.service</string> <key>NSExtensionPrincipalClass</key> - <string>NotificationService</string> + <string>$(PRODUCT_MODULE_NAME).NotificationService</string> </dict> </dict> </plist> diff --git a/JuickPush/JuickPush-Bridging-Header.h b/JuickPush/JuickPush-Bridging-Header.h new file mode 100644 index 0000000..1b2cb5d --- /dev/null +++ b/JuickPush/JuickPush-Bridging-Header.h @@ -0,0 +1,4 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + diff --git a/JuickPush/NotificationService.h b/JuickPush/NotificationService.h deleted file mode 100644 index 91ca27a..0000000 --- a/JuickPush/NotificationService.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// NotificationService.h -// JuickPush -// -// Created by Vitaly Takmazov on 18/10/2018. -// Copyright © 2018 com.juick. All rights reserved. -// - -#import <UserNotifications/UserNotifications.h> - -@interface NotificationService : UNNotificationServiceExtension - -@end diff --git a/JuickPush/NotificationService.m b/JuickPush/NotificationService.m deleted file mode 100644 index afeec55..0000000 --- a/JuickPush/NotificationService.m +++ /dev/null @@ -1,46 +0,0 @@ -// -// NotificationService.m -// JuickPush -// -// Created by Vitaly Takmazov on 18/10/2018. -// Copyright © 2018 com.juick. All rights reserved. -// - -#import "NotificationService.h" - -@interface NotificationService () - -@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); -@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; - -@end - -@implementation NotificationService - -- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { - self.contentHandler = contentHandler; - self.bestAttemptContent = [request.content mutableCopy]; - - NSURL *avatarURL = [NSURL URLWithString:request.content.userInfo[@"avatarUrl"]]; - - NSURLSession* downloadSession = [NSURLSession sharedSession]; - - [[downloadSession downloadTaskWithURL:avatarURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { - if (!error) { - NSError *err = nil; - NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:[response suggestedFilename]]; - [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:temporaryDirectory error:&err]; - UNNotificationAttachment *avatarAttachment = [UNNotificationAttachment attachmentWithIdentifier:avatarURL.lastPathComponent URL:[NSURL fileURLWithPath:temporaryDirectory] options:nil error:&err]; - self.bestAttemptContent.attachments = @[avatarAttachment]; - self.contentHandler(self.bestAttemptContent); - } - }] resume]; -} - -- (void)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. - self.contentHandler(self.bestAttemptContent); -} - -@end 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) + } + } +} |