[Xcode] 시뮬레이터에서 Remote Push 시뮬레이션하기

2020-02-11

Xcode 11.4부터 시뮬레이터에서 Remote Push Notification의 시뮬레이션을 지원합니다. 😄

시뮬레이션을 하는 방법은 다음과 같이 두가지가 있습니다.

  1. 시뮬레이터에 apns 파일을 직접 드래그 하는 방법
  2. CLI를 이용하는 방법

준비

Remote Push Notification를 시뮬레이션 하기 위해서는 우선 사용자에게 푸시 알림 사용 권한을 요청하고 허가를 받아야 합니다.

권한 요청을 위해 AppDelegate 파일의 didFinishLaunchingWithOptions 델리게이트에 권한 요청 코드를 추가합니다.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   requestAuthorizationForRemotePushNotification()
   return true
}

// 사용자에게 푸시 권한을 요청
func requestAuthorizationForRemotePushNotification() {
    let current = UNUserNotificationCenter.current()
    current.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
    	// granted가 true로 떨어지면 푸시를 받을 수 있습닏.
    }
}

앱을 실행하면 권한 요청 팝업이 노출되는데, 허용(Allow)을 선택해 Remote Push를 사용할 수 있도록 해줍니다.

Request Permission

준비는 끝났습니다. 이제 시뮬레이션을 해보죠.

1. 시뮬레이터에 apns 파일을 직접 드래그 하는 방법

apns 파일은 json 포맷의 확장자가 apns인 파일입니다.

apns의 payload 형식을 따라 샘플 파일을 생성합니다.

{
    "Simulator Target Bundle" : "com.joyfulpace.apnsTest",
    "aps" : {
        "alert" : {
            "title" : "최고의 iOS 개발팀 맴버",
            "body" : "쉘든, 제이든, 플라, 네오, 줄리, 셀리나, 루키, 헤이, 세일러, 바트, 스티브, 가브리엘, 딘, 조나단, 토미",
        },
    },
}

paylaod의 key는 다음과 같이 크게 두 부분으로 구성돼 있습니다.

  1. Simulator Target Bundle : 시뮬레이터에 설치된 테스트 앱의 bundleID
  2. aps : Remote Push Notification 사용을 위해 지정된 payload (더 많은 payload key는 이곳에서 확인할 수 있습니다.)

알맞은 앱 bundleID와 payload를 넣고 파일을 저장합니다. 저는 이 파일을 test.apns 로 저장하겠습니다.

이제 이 파일을 시뮬레이터에 드래그 해보겠습니다.

Drag

시뮬레이션이 잘 되는 것을 확인할 수 있습니다.

2. CLI를 사용하는 방법

터미널에서도 푸시 시뮬레이션을 할 수 있습니다.

시뮬레이션에는 xcrun 명령을 사용합니다.

명령어는 다음과 같습니다.

xcrun simctl push EEDF7FDD-6C34-4DB6-8A3B-43B71769E2D8 com.joyfulpace.apnsTest test.apns

[xcrun 명령]

$ xcrun simctl push --help
Send a simulated push notification
Usage: simctl push <device> [<bundle identifier>] (<json file> | -)

	bundle identifier
	     The bundle identifier of the target application
	     If the payload file contains a 'Simulator Target Bundle' top-level key this parameter may be omitted.
	     If both are provided this argument will override the value from the payload.
	json file
	     Path to a JSON payload or '-' to read from stdin. The payload must:
	       - Contain an object at the top level.
	       - Contain an 'aps' key with valid Apple Push Notification values.
	       - Be 4096 bytes or less.

Only application remote push notifications are supported. VoIP, Complication, File Provider, and other types are not supported.

파라미터에서 입력해 줘야하는 내용은 다음과 같습니다.

  • device : 시뮬레이터의 Identifier
  • bundle identifier : 자신의 앱의 번들 ID
  • json file : 확장자가 apns여도 상관 없음

시뮬레이터의 Identifier는 다음과 같이 xcrun simctl list devices 명령으로 찾을 수 있습니다. 리스트 중에서 현재 사용중인 시뮬레이터는 Booted라고 상태가 표시됩니다. 시뮬레이터 기종이 맞고 상태가 Booted인 시뮬레이터 identifier를 복사해 사용하면 됩니다.

Device List

다음과 같이 grep Booted로 list의 결과를 필터링해 보면 한번에 찾을 수 있습니다.

$ xcrun simctl list devices | grep Booted
    iPhone 11 Pro Max (EEDF7FDD-6C34-4DB6-8A3B-43B71769E2D8) (Booted) 

CLI를 사용할 경우 다음과 같이 apns 파일에 Simulator Target Bundle 부분을 생략할 수 있습니다. bundleID가 CLI의 파라미터에 들어가기 때문이죠.

{
    "aps" : {
        "alert" : {
            "title" : "최고의 iOS 개발팀 맴버",
            "body" : "쉘든, 제이든, 플라, 네오, 줄리, 셀리나, 루키, 헤이, 세일러, 바트, 스티브, 가브리엘, 딘, 조나단, 토미",
        },
    },
}

실행해 보겠습니다. Yay! 😙 잘 됩니다.

CLI

이상 시뮬레이터에서 Remote Push Notification을 시뮬레이션 하는 방법에 대해 알아 보았습니다. 😄



[책] 토미의 Git with 소스트리

Git을 제대로 알고 싶으신 분들께 추천드립니다.