summaryrefslogtreecommitdiff
path: root/JuickPush
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-09-16 17:39:47 +0300
committerGravatar Vitaly Takmazov2023-05-14 01:15:35 +0300
commit7165263963d046fe8bd8a8abb542cb0bb34866b9 (patch)
tree8f464b64432aa4b609e7f3b15f5eaa025cf1bd68 /JuickPush
parent7a36ade3636ede2781630b2d2d5e77fb82b39d69 (diff)
NotificationService rewritten in Swift, added dummy test target
# Conflicts: # Juick.xcodeproj/project.pbxproj # JuickPush/NotificationService.m
Diffstat (limited to 'JuickPush')
-rw-r--r--JuickPush/Info.plist2
-rw-r--r--JuickPush/JuickPush-Bridging-Header.h4
-rw-r--r--JuickPush/NotificationService.h13
-rw-r--r--JuickPush/NotificationService.m46
-rw-r--r--JuickPush/NotificationService.swift48
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)
+ }
+ }
+}