Notification Service Extension | Appbox iOS SDK 가이드

Notification Service Extension

푸시 이미지나 수신 집계를 쓰려면 NSE target이 따로 필요합니다. 이 장은 그 target의 NotificationService 구현 코드를 다룹니다.
동작하는 구성은 AppBoxSampleiOS의 appBoxNotiServiceExtension에서 확인할 수 있습니다.
서버 payload 설정은 다루지 않습니다. NSE에서는 AppBoxPushSDK의 공개 진입점을 사용합니다.

왜 필요한가

  • 푸시에 이미지를 붙여 보이고, 앱박스 콘솔의 수신 기록, 사용자 여정, 도달률을 쌓으려면 알림 서비스 확장(NSE)이 필요합니다.
  • 확장이 없어도 알림은 보이지만 이미지는 보이지 않습니다.

만들기

  • 1. Xcode › File › New › Target › Notification Service Extension 을 추가합니다.
  • 2. 확장 타겟에 AppBoxPushSDK 를 연결합니다(앱 타겟과 같은 방법).
  • 3. 아래 항목 정의의 App Group 을 앱 타겟과 확장 타겟 둘 다에 켭니다.
  • 4. 확장의 NotificationService.swift 를 아래 예제 코드로 바꿉니다.

Notification Service Extension

Swift
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
  • 동일한 Push 요청의 기록 함수를 main app과 NSE 양쪽에서 중복 호출하지 않습니다.
  • 이 저장소 공유에는 앱 target과 NSE target에 App Groups capability가 필요합니다.
    App Group ID는 Bundle ID가 아니라 group. 접두가 붙는 값입니다. 예: group.com.example.app

App Group

무엇에 필요한가필요 여부
수신 기록, 사용자 여정 수신, 도달률(recordNotificationReceived, recordJourneyPushReceived, recordPushDelivered)필요. 없으면 오류 없이 기록을 건너뜁니다
알림 표시, 이미지(createFCMImage)필요 없음

App Group ID 규칙 (위에서부터 먼저 적용)

  • 1. 코드에서 configureAppGroupIdentifier(_:) 로 정한 값
  • 2. Info.plist 의 AppBoxAppGroupIdentifier 값
  • 3. 둘 다 없으면 group.<앱 번들 ID>. 확장의 번들 ID 가 <앱 번들 ID>.NotificationService 처럼 앱 번들 ID 로 시작하면 끝을 떼고 같은 값으로 계산합니다.
  • 가장 쉬운 방법: Signing & Capabilities › App Groups 에서 앱과 확장 모두 group.<앱 번들 ID> 하나를 켭니다. 예: 앱 번들 ID 가 com.example.shop 이면 group.com.example.shop
  • 다른 이름을 쓰면 앱과 확장 모두 Info.plist 에 AppBoxAppGroupIdentifier 로 같은 값을 넣어 주세요.
  • createFCMImage 는 이미지가 없어도 결과를 돌려줍니다. 그래서 예제처럼 contentHandler 를 한 번만 부르게 막아야 합니다.
www.naver.com
QR 코드를 촬영하면 입력하신 주소의
앱 예시 화면을 테스트할 수 있습니다.
* 스캔 후 바로 앱 다운로드 이동합니다.