Skip to content

Instantly share code, notes, and snippets.

@skyofdwarf
Created December 12, 2022 14:20
Show Gist options
  • Save skyofdwarf/b1de48f9c8f4cb3585d5c34bd75cbf59 to your computer and use it in GitHub Desktop.
Save skyofdwarf/b1de48f9c8f4cb3585d5c34bd75cbf59 to your computer and use it in GitHub Desktop.
리모트 노티피케이션 정리

시간이 흘러 이제 iOS 16이 나왔고, 이제는 iOS 10에 추가된 UNUserNotificationCenter를 이용해 로직만 사용하는게 당연하다. 하지만 레거시 코드들에는 사용하지 않는 예전 푸시 관련 코드들이 같이 남아 있어, 간만에 푸시작업을 할 떄마다 헷갈리게 만든다.

UIApplicationDelegate의 url-open 관련 메시지들도...

때문에 n년 후의 나를 위해 정리를 남긴다.

리모트 노티피케이션 사용 절차

  1. APNS 등록

    APNS에 기기등록을 위해 UIApplication.registerForRemoteNotifications 호출

  2. UI/사용자 상호작용을 위한 권한 등록

    banner, alarm 등을 위해 UNUserNotificationCenter.requestAuthorization(...)을 호출

    UI/사용자 인터렉션이 필요없다면 1번만으로!

푸시 종류별 호출 메시지

일반 푸시

  • 앱이 foreground 상태에서 푸시 수신 시
    1. UNUserNotificationCenterDelegate.userNotificationCenter(_:willPresent:withCompletionHandler:) 호출되고 completionHandler 호출 값에 따라 배너가 노출된다.
    2. 사용자가 이 배너를 선택한다면 이어서 UNUserNotificationCenterDelegate.userNotificationCenter(_:didReceive:withCompletionHandler:) 가 호출된다.
  • 앱이 foreground 상태가 아닐 때 수신 시 수신한 푸시를 선택해 앱이 실행되면 UNUserNotificationCenterDelegate.userNotificationCenter(_:didReceive:withCompletionHandler:) 가 호출된다.

백그라운드 푸시

앱이 fore/background 상관없이 백그라운드 푸시('content-available: 1')를 받는다면 UIApplicationDelegate.application(_:performFetchWithCompletionHandler:) 이 호출된다.

이 메시지는 iOS 13에서 deprecated 됐고, BGAppRefreshTask 를 사용하라니 BGAppRefreshTask는 나중에 추가적으로 알아보자(니가)

추가로, UNUserNotificationCenter을 사용하면 레거시코드에 자주 남아있고 이전에 사용(호출)되던 UIApplicationDelegate.application(_:performFetchWithCompletionHandler:) 이 당연히 호출되지 않는다.

여기서 궁금, UNUserNotificationCenter.requestAu.. 했지만 UNUserNotificationCenterDelegate.userNotificationCenter(_:didReceive:withCompletionHandler:) 메시지 구현을 안하면 이전 이 메시지가 호출될까? 확인해 보자(니가)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment