푸시메시지 이미지 첨부

📘

지원 SDK 버전

iOS SDK 2.53.0 버전 이상

iOS 앱에서 이미지를 포함한 푸시 메시지를 보여주기 위해서는 Notification Service Extension을 추가하여 아래의 설정을 완료합니다.

iOS Rich Push Notification 에 대한 자세한 사항은 Rich Push Notification 에서 확인 가능합니다.

1. 앱에 Notification Service Extension 추가

Xcode 프로젝트 상단 File > New > Target... 탭을 선택하여 아래와 같이 Notification Service Extension을 선택합니다.

알맞은 이름을 입력 후 Finish를 눌러주세요.

CocoaPods 설정

CocoaPods을 이용해 핵클 SDK를 추가한 경우 Podfile에 앞서 추가한 Extension을 다음과 같이 구성합니다.

use_frameworks!

target 'sampleapp' do
  pod 'Hackle', '~> 2.28.0'
end

target 'NotificationServiceExtension' do
  pod 'Hackle', '~> 2.28.0'
end

Swift Package Manager 설정

Swift Package Manager를 이용해 핵클 SDK를 추가한 경우 앞서 추가한 ExtensionHackle 프레임워크를 추가합니다.

2. Minimum Deployment 설정

Notification Service Extension은 앱과 별도로 최소 지원버전을 명시해야 합니다.

Minimum Deployment를 앱과 동일하게 설정하는 것을 추천합니다.

❗️

최소 지원 버전이 앱과 extension이 다를 경우, 앱 버전에 따라 이미지가 표시되지 않을 수 있습니다.

ex) App 최소 지원 버전이 iOS 15, Extension 최소 지원 버전이 iOS 18인 경우

  • iOS 15 이상, iOS 18 미만 버전은 이미지가 표시되지 않습니다.
  • iOS 18 이상 버전은 이미지가 표시됩니다.

3. 핵클 SDK와 연동하기

푸시 메시지에 이미지 추가

푸시 이미지 처리를 위해 didReceive 함수에서 handleRichNotification 함수를 추가합니다.

import UserNotifications
import Hackle

class NotificationService: UNNotificationServiceExtension {

    var defaultNotificationContent: UNNotificationContent?
    var contentHandler: ((UNNotificationContent) -> Void)?

    override func didReceive(
      _ request: UNNotificationRequest, 
      withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
    ) {
        self.defaultNotificationContent = request.content
        self.contentHandler = contentHandler
        
        if Hackle.handleRichNotification(request: request, contentHandler: contentHandler) {
            return
        }

        contentHandler(request.content)
    }
    
    override func serviceExtensionTimeWillExpire() {
      if let contentHandler = contentHandler, 
         let defaultNotificationContent = defaultNotificationContent {
            contentHandler(defaultNotificationContent)
        }
    }
}
#import <UserNotifications/UserNotifications.h>
@import Hackle;

@interface NotificationService : UNNotificationServiceExtension

@end
#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 *))contentHandler {
    self.defaultNotificationContent = request.content;
    self.contentHandler = contentHandler;
    
    if ([Hackle handleRichNotificationWithRequest:request contentHandler:contentHandler]) {
        return;
    }
    contentHandler(request.content);
}

- (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