iOS Push & Background Push

Jinhyuk
2 min readApr 13, 2020

Push Notification 의 경우, 일반적으로 화면에 노출되는 Push와 유저들에게 노출되지 않는 Background Push 2종류의 푸시가 존재한다.
3가지의 테스트에 따라 Push가 어떤 메소드를 통해 전달되는지 확인해본다.

일반 푸시 : Project -> Signing&Capability -> Push Notification 체크
백그라운드 푸시 : Project -> Signing&Capability -> BackGround Mode -> Background Fetch 체크

1. 앱이 실행중인 경우 (기기 화면에 앱이 보이는 경우)

· 일반푸시

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler

· 백그라운드 푸시

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

2. 앱이 백그라운드에 있는 경우

· 일반 푸시

o 해당 푸시를 터치하여 앱에 들어올 경우

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __API_AVAILABLE(macos(10.14), ios(10.0), watchos(3.0)) __API_UNAVAILABLE(tvos)

o 해당 푸시를 터치하지 않고 앱이 백그라운드에 있는 경우

  • 데이터 받을수 없음

o 해당 푸시만 받는 경우

  • 데이터 받을수 없음

· 백그라운드 푸시

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

3. 앱이 완전히 종료된 후 푸시를 받는 경우

· 일반 푸시

o 해당 푸시를 터치하여 들어올 경우

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions... NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
해당 메소드로 푸시 정보를 받을수 있음
//결과값
userInfo->{
alert = {
body = “Your message Here”;
title = afasdf;
};
sound = default;
}

o 해당 푸시를 터치하지 않고 앱을 실행한 경우

  • 데이터 받을수 없음

· 백그라운드 푸시

  • 데이터 받을수 없음

일반푸시 전송 테스트 방법

Easy APNs Provider’ 를 사용하여 간단히 테스트가 가능하다.

백그라운드 푸시 전송 테스트 방법

iOS12 이하의 경우, Easy APNs Provider 를 사용하여 테스트가 가능하다.
iOS 13 이상의 경우, ‘apns-topic’ , ‘apns-push-type’ 를 반드시 헤더에 추가해주어야 하기에 위의 방법으로는 불가능하다.

CURL 사용하여 백그라운드 푸시 보내는 방법

curl -v -d ‘{“aps” : {“content-available” : 1}’ \
-H “
apns-topic: {Your BundleID}” \
-H “
apns-push-type:background” \
— http2 \
— cert /Users/jinhyuk/Desktop/apns-dev.pem \
https://api.development.push.apple.com/3/device/{Your Device Token}

테스트를 위한 .pem 파일 생성 방법

KeychainAccess에서 IOS 푸시메시지용 인증서(apns-dev-cert.p12)와 키(apns-dev-key.p12) 두개의 파일을 생성openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p1openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pemcat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem

FCM으로 Background Push 보내는 방법

위와 마찬가지로 FCM 콘솔에선 Background Push를 보낼수 없기에
PostMan과 같은 툴을 사용한 방법입니다.
(iOS 버전 상관없음!!)

FCM Push

POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: key=[FCM Server key]
{
“to”: “[FCM Token]”,
“content_available”: true,

“data”: {
“{Your Custom Data}”: 1
}
}

--

--