1 import Foundation
2 import UserNotifications
3 import AppBoxPushSDK
4
5 final class NotificationService: UNNotificationServiceExtension {
6 private let completionLock = NSLock()
7 private var didComplete = false
8 private var contentHandler: ((UNNotificationContent) -> Void)?
9 private var fallbackContent: UNNotificationContent?
10
11 override func didReceive(
12 _ request: UNNotificationRequest,
13 withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
14 ) {
15 completionLock.lock()
16 didComplete = false
17 self.contentHandler = contentHandler
18 fallbackContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
19 ?? request.content
20 completionLock.unlock()
21
22 // 수신 기록(앱에서 다시 부르지 않습니다)
23 AppBoxPush.shared.recordNotificationReceived(request)
24 AppBoxPush.shared.recordJourneyPushReceived(request)
25 AppBoxPush.shared.recordPushDelivered(request)
26
27 // 이미지가 있으면 내려받아 붙입니다
28 AppBoxPush.shared.createFCMImage(request) { [self] content in
29 complete(with: content)
30 }
31 }
32
33 override func serviceExtensionTimeWillExpire() {
34 complete(with: nil) // 시간이 다 되면 원래 내용으로
35 }
36
37 private func complete(with content: UNNotificationContent?) {
38 completionLock.lock()
39 guard !didComplete,
40 let contentHandler,
41 let resolvedContent = content ?? fallbackContent else {
42 completionLock.unlock()
43 return
44 }
45 didComplete = true
46 self.contentHandler = nil
47 fallbackContent = nil
48 completionLock.unlock()
49 contentHandler(resolvedContent)
50 }
51 }
1 // NotificationService.h
2 #import <UserNotifications/UserNotifications.h>
3
4 @interface NotificationService : UNNotificationServiceExtension
5 @end
6
7 // NotificationService.m
8 #import "NotificationService.h"
9 @import AppBoxPushSDK;
10
11 @interface NotificationService ()
12
13 @property (nonatomic, copy, nullable) void (^contentHandler)(UNNotificationContent *contentToDeliver);
14 @property (nonatomic, strong, nullable) UNNotificationContent *fallbackContent;
15 @property (nonatomic, assign) BOOL didComplete;
16
17 @end
18
19 @implementation NotificationService
20
21 - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
22 @synchronized (self) {
23 self.didComplete = NO;
24 self.contentHandler = contentHandler;
25 self.fallbackContent = [request.content mutableCopy] ?: request.content;
26 }
27
28 // 수신 기록(앱에서 다시 부르지 않습니다)
29 [AppBoxPush.shared recordNotificationReceived:request];
30 [AppBoxPush.shared recordJourneyPushReceived:request];
31 [AppBoxPush.shared recordPushDeliveredWithRequest:request];
32
33 // 이미지가 있으면 내려받아 붙입니다
34 [AppBoxPush.shared createFCMImage:request withContentHandler:^(UNNotificationContent * _Nonnull content) {
35 [self completeWithContent:content];
36 }];
37 }
38
39 - (void)serviceExtensionTimeWillExpire {
40 [self completeWithContent:nil]; // 시간이 다 되면 원래 내용으로
41 }
42
43 // contentHandler 는 정확히 한 번만 부릅니다
44 - (void)completeWithContent:(nullable UNNotificationContent *)content {
45 void (^handler)(UNNotificationContent *) = nil;
46 UNNotificationContent *resolvedContent = nil;
47
48 @synchronized (self) {
49 resolvedContent = content ?: self.fallbackContent;
50 if (self.didComplete || self.contentHandler == nil || resolvedContent == nil) {
51 return;
52 }
53 self.didComplete = YES;
54 handler = self.contentHandler;
55 self.contentHandler = nil;
56 self.fallbackContent = nil;
57 }
58
59 handler(resolvedContent);
60 }
61
62 @end