티스토리 뷰

반응형

 

Xcode 14 릴리스노트를 다시 보던 중 

Simulator now supports remote notifications in iOS 16 when running in macOS 13 on Mac computers with Apple silicon or T2 processors. Simulator supports the Apple Push Notification Service Sandbox environment. Your server can send a remote notification to your app running in that simulator by connecting to the APNS Sandbox (api.sandbox.push.apple.com). Each simulator generates registration tokens unique to the combination of that simulator and the Mac hardware it’s running on. 

이제 시뮬레이터는 Apple 실리콘 또는 T2 프로세서가 탑재된 Mac 컴퓨터에서 macOS 13으로 실행할 때 iOS 16에서 원격 알림을 지원합니다. 시뮬레이터는 Apple 푸시 알림 서비스 샌드박스 환경을 지원합니다.
서버는 APNS 샌드박스(api.sandbox.push.apple.com)에 연결하여 해당 시뮬레이터에서 실행 중인 앱에 원격 알림을 보낼 수 있습니다.
각 시뮬레이터는 해당 시뮬레이터와 시뮬레이터가 실행 중인 Mac 하드웨어의 조합에 고유한 등록 토큰을 생성합니다.


이런게 있어서 자세히 살펴보려고 한다.

핵심은 시뮬레이터에서 Remote notificaton을 지원한다는 것인데, 

보통 Xcode 11.4 ) Remote Push Notifications in Simulator (feat. APNs file) 에서 본 것 처럼

.apns 파일을 끌어다 놓는식(drag and drop)으로  테스트 했는데 


이제 진짜로 시뮬레이터에 Remote notification을 보낼 수 있다니!! 


 

# 준비물

1. Apple 실리콘 또는 T2 프로세서가 탑재된 Mac 컴퓨터

2. macOS 13 (Ventura) 

3. Xcode 14

 

# 시뮬레이터에 Remote Notification 보내보기 

 

1. 프로젝트 만들기 (Xcode 14+로) 

나는 대충 ZeddPushTest로 만들어줬따..

 

2. Capabilities > Push Notification 추가

 

3. AppDelegate.swift 수정

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.reduce("") {
        $0 + String(format: "%02X", $1)
    }
    print(token)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print(error.localizedDescription)
}

DeviceToken 얻어오는 부분 추가해주기

 

4. 알림 권한 요청 

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {
    didAllow,Error in

    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
})

 

5. 빌드

 

6. DeviceToken 확인

DeviceToken이 무사히 나왔는지 확인해야한다. 잘 나왔다면 복사 ㄱㄱ

평소보다(?) 살짝 길텐데 아래와 같은 이유때문인 것 같다. 

각 시뮬레이터는 해당 시뮬레이터와 시뮬레이터가 실행 중인 Mac 하드웨어의 조합에 고유한 등록 토큰을 생성합니다.

 

7. DeviceToken을 가지고 Remote Notification 보내보기

사실 나는 Sending push notifications using command-line tools (feat. Token / .p8) 글 쓸때 만들어둔 프로젝트와 스크립트가 있어서 그대로 썼다. 

FCM을 이용하든 커맨드라인툴로 하든 이미 세팅이 끝난 상태라면 DeviceToken만 있으면 되니까....ㅎ;

DeviceToken입력하는 부분에 복사한 시뮬레이터 DeviceToken을 넣어준다.

나의 경우 👇 와 같음 

TEAM_ID=Team ID
TOKEN_KEY_FILE_NAME=path to the private key file
AUTH_KEY_ID=your key identifier
TOPIC=App ID
DEVICE_TOKEN=복사한 시뮬레이터 Device Token 넣어주기 ✅
APNS_HOST_NAME=api.sandbox.push.apple.com

주의 할 점은

서버는 APNS 샌드박스(api.sandbox.push.apple.com)에 연결하여 해당 시뮬레이터에서 실행 중인 앱에 원격 알림을 보낼 수 있습니다.


때문에

TEAM_ID=Team ID
TOKEN_KEY_FILE_NAME=path to the private key file
AUTH_KEY_ID=your key identifier
TOPIC=App ID
DEVICE_TOKEN=복사한 시뮬레이터 Device Token 넣어주기
APNS_HOST_NAME=api.sandbox.push.apple.com ✅

HOST는 반드시 sandbox여야 하는 것 같다. (아니면 Bad Device Token 에러남) 

 

8. Remote notification이 잘 오는지 확인

야호

 

# 테스트하면서 이상했던 점

Xcode 14.2에서 시뮬레이터에서 DeviceToken이 안나오는 경우가 있었다.

분명히 예전에 테스트했을 때는 나왔는데.. 갑자기 안나오는게 너무 이상해서 다른 시뮬레이터로 테스트하니까 나옴

다른 시뮬레이터에서 테스트하다가 안된 시뮬레이터에서 다시 해보니까 또 이제는 나온다.

DeviceToken이 안나온다면 그냥 다른 시뮬레이터로 일단 해보는 것을 추천...ㅎ

 

그리고 처음에는 푸시를 보내니까 바로 안왔었는데, 이것도 너무 이상해서;; 뭐지.. 하고 삽질하는 동안 푸시가 왔다. 

그 뒤로는 스크립트 실행하자마자 잘 옴 ㅎ;

 

 

[참고]

https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

반응형