Registering Your App with APNs

Panther·2021년 8월 17일
0

https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns

"Communicate with Apple Push Notification service (APNs) and receive a unique device token that identifies your app."

애플 푸시 노티피케이션 서비스(APNs)와 커뮤니케이션하고, 앱을 식별하는 고유한 기기 토큰을 받습니다.

Overview

애플 푸시 노티피케이션 서비스(APNs)는 사용자 기기에 노티피케이션을 보낼 수 있기 전에 사용자 기기의 주소를 알아야 합니다. 이 주소는 기기 및 앱에서 고유한 기기 토큰의 형태를 받습니다. launch 타임에 앱은 APNs와 커뮤니케이션하고 APNs의 기기 토큰을 받습니다. 이후 제공자 서버에 전달하게 되는 토큰입니다. 서버는 보내고자 하는 모든 노티피케이션에 해당 토큰을 포함시켜야 합니다.

Note
하나의 앱에 대한 기기 토큰은 다른 앱에서 사용될 수 없으며, 두 앱이 같은 기기에 설치되어 있어도 그렇습니다. 두 앱은 각자 고유한 기기 토큰을 요청해야 하고, 제공자 서버에 전달해야 합니다.

Enable the Push Notifications Capability

앱에서 필요한 권한을 추가하려면 Figure 1에서 보이는 것처럼 푸시 노티피케이션 기능을 활성화해야 합니다. iOS에서 이 옵션을 호라성화하는 것은 앱에 APS Environment Entitlement를 추가합니다. macOS에서는 APS Environment (macOS) Entitlement를 추가합니다. Enable push notifications를 보시기 바랍니다.

Enable push notifications
https://help.apple.com/xcode/mac/current/#/devdfd3d04a1

Figure 1 Enabling the push notifications capability

Important
개발자 계정에서 프로젝트에 할당된 앱 ID에 대한 푸시 노티피케이션 서비스 역시 활성화해야 합니다. 개발자 계장 설정에 대한 더 많은 내용은 Developer Account 페이지로 가시기 바랍니다.

Developer Account
https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=891bd3417a7776362562d2197f89480a8547b108fd934911bcbea0110d07f757&path=%2Faccount%2F&rv=1#/overview/

Register Your App and Retrieve Your App's Device Token

APNs와 함께 앱을 등록하고 전역적으로 고유한 기기 토큰을 받아야 합니다. 토큰은 햔재 앱에 있는 주소입니다. 제공자 서버는 기기에 노티피케이션을 전달할 수 있기 전에 이 토큰을 갖고 있어야 합니다.

애플이 제공한 API를 사용해서 앱이 launch될 때마다 앱을 등록할 수 있고 기기 토큰을 받을 수 있습니다. 등록 과정은 플랫폼에 걸쳐 유사합니다.

  • iOS 및 tvOS에서 기기 토큰을 요청하려면 UIApplicationregisterForRemoteNotifications() 메소드를 호출해야 합니다. 등록이 성공적이면 앱 딜리게이트의 application(_:didRegisterForRemoteNotificationsWithDeviceToken:) 메소드에서 토큰을 받습니다.
  • macOS에서는 기기 토큰을 요청하려면 NSApplicationregisterForRemoteNotifications()를 호출해야 합니다. 등록이 성공적이면 앱 딜리게이트의 application(_:didRegisterForRemoteNotificationsWithDeviceToken:) 메소드에서 토큰을 받습니다.

APNs로 성공적인 등록을 처리하는 것과 더불어 application(_:didFailToRegisterForRemoteNotificationsWithError:) 메소드를 구현해서 등록이 실패한 경우도 처리할 준비를 해야 합니다. 사용자의 기기가 네트워크에 연결되어 있지 않는 경우, APNs 서버가 어떠한 이유로 닿을 수 없는 경우, 앱이 적합한 code-signing 자격을 갖지 못하는 경우에 등록이 실패할 것입니다. 실패가 발생하면 플래그를 설정하고 이후에 다시 등록을 시도하시기 바랍니다.

Listing 1은 원격 노티피케이션 등록 및 상응하는 토큰을 받기 위해 필요한 iOS 앱 딜리게이트 메소드의 샘플 구현을 보여주고 있습니다. sendDeviceTokenToServer 메소드는 앱이 제공자 서버에게 데이터를 보내기 위해 사용하는 커스텀 메소드입니다.

Listing 1 Communicating with APNs at launch time

func application(_ application: UIApplication,
           didFinishLaunchingWithOptions launchOptions:
           [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   // Override point for customization after application launch.you’re         
   UIApplication.shared.registerForRemoteNotifications()
   return true
}

func application(_ application: UIApplication,
            didRegisterForRemoteNotificationsWithDeviceToken 
                deviceToken: Data) {
   self.sendDeviceTokenToServer(data: deviceToken)
}

func application(_ application: UIApplication,
            didFailToRegisterForRemoteNotificationsWithError 
                error: Error) {
   // Try again later.
}

Important
로컬 저장소에 기기 토큰을 캐싱은 절대로 하지 않아야 합니다. APNs는 사용자가 백업으로부터 기기를 복구할 때, 사용자가 앱을 새 기기에 설치할 때, 사용자가 운영체제를 다시 설치할 때 새 토큰을 발행합니다. 때마다 토큰 제공을 시스템에 요청하면 최신 토큰을 가져오는 것에 대해 보장받습니다.

Forward Tokens to Your Provider Server

기기 토큰을 받으면 앱과 제공자 서버의 네트워크 연결을 열어야 합니다. 보안상 안전하게 기기 토큰 및 특정 사용자 식별에 필요한 다른 정보를 서버에 전달해야 합니다. 예를 등러 사용자의 로그인 이름 혹은 서비스에 연결하는 어떤 것을 포함시킬 것입니다. 네트워크를 통해서 보내는 모든 정보는 암호화해야 합니다.

제공자 서버에서 노티피케이션을 보내기 위한 토큰을 개발자가 접근할 수 있으면서도 안전한 장소에 저장하시기 바랍니다. 노티피케이션을 생성할 때 서버는 특정 기기에 노티피케이션을 보낼 수 있어야 합니다. 노티피케이션이 사용자의 계정에 연결되는 경우 사용자의 계정 정보와 함께 기기 토큰을 저장해야 합니다. 사용자는 여러 기기를 가질 수 있기 때문에 여러 기기 토큰을 처리할 수 있도록 앱을 준비해야 합니다.

페이로드 및 기기 토큰을 APNs에 보내는 방법에 대한 더 많은 정보는 Sending Notification Requests to APNs를 보시기 바랍니다.

Sending Notification Requests to APNs
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
https://velog.io/@panther222128/Sending-Notification-Requests-to-APNs

See Also


Remote Notifications

Setting Up a Remote Notification Server

노티피케이션을 생성하고 사용자 기기로 푸시합니다.

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server
https://velog.io/@panther222128/Setting-Up-a-Remote-Notification-Server

Sending Push Notifications Using Command-Line Tools

애플 푸시 노티피케이션 서비스(APNs)에 푸시 노티피케이션을 전송하기 위해 기본적인 macOS 커맨드 라인 툴을 사용합니다.

https://developer.apple.com/documentation/usernotifications/sending_push_notifications_using_command-line_tools
https://velog.io/@panther222128/Sending-Push-Notifications-Using-Command-Line-Tools


0개의 댓글