From 7165263963d046fe8bd8a8abb542cb0bb34866b9 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Wed, 16 Sep 2020 17:39:47 +0300
Subject: NotificationService rewritten in Swift, added dummy test target
# Conflicts:
# Juick.xcodeproj/project.pbxproj
# JuickPush/NotificationService.m
---
JuickPush/Info.plist | 2 +-
JuickPush/JuickPush-Bridging-Header.h | 4 +++
JuickPush/NotificationService.h | 13 ----------
JuickPush/NotificationService.m | 46 ---------------------------------
JuickPush/NotificationService.swift | 48 +++++++++++++++++++++++++++++++++++
5 files changed, 53 insertions(+), 60 deletions(-)
create mode 100644 JuickPush/JuickPush-Bridging-Header.h
delete mode 100644 JuickPush/NotificationService.h
delete mode 100644 JuickPush/NotificationService.m
create mode 100644 JuickPush/NotificationService.swift
(limited to 'JuickPush')
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 @@
NSExtensionPointIdentifier
com.apple.usernotifications.service
NSExtensionPrincipalClass
- NotificationService
+ $(PRODUCT_MODULE_NAME).NotificationService
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
-
-@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)
+ }
+ }
+}
--
cgit v1.2.3