iOS 응용 (swift)
[iOS - Swift] Push Notification 처리 관련 메소드 총 정리(userInfo, 딥링크) - didFinishLaunchingWithOptions, didReceiveRemoteNotification, willPresent, didReceive, open url
jake-kim
2022. 10. 8. 23:38
UserInfo란?
- 푸시 데이터가 담겨있는 json 형태를 [AnyHashable: Any]형태로 있는 자료형
{"aps":{"alert":"test","badge":"1"}}
ex) 시스템 푸시를 클릭했을때 동작하는 델리게이트 메소드에서 userInfo 획득
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo = response.notification.request.content.userInfo
print(userInfo)
}
UserInfo를 받는 경로와 DeepLink 처리
- application(_:didFinishLaunchingWithOptions:)
- 앱이 종료되었을때 시스템 노티를 탭한 경우, 여기서 UserInfo와 DeepLink처리가 가능
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let userInfo = (launchOptions?[.remoteNotification] as? [String: Any])
print(userInfo)
}
- application(:didReceiveRemoteNotification:)
- 앱이 종료되었을때 시스템 노티를 탭한 경우 이곳에서도 수신
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
print(userInfo)
}
- userNotificationCenter(_:willPresent:withCompletionHandler:)
- foreground에서 시스템 푸시를 수신했을 때 해당 메소드가 호출
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
let userInfo = notification.request.content.userInfo
print(userInfo)
completionHandler([.alert, .badge, .sound])
}
- userNotificationCenter(_:didReceive:withCompletionHandler:)
- foreground, background에서 시스템 푸시를 탭하거나 dismiss했을때 해당 메소드가 호출
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo = response.notification.request.content.userInfo
print(userInfo)
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print("시스템 푸시 탭!")
}
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print("시스템 푸시 dismiss")
}
}
- application(_:open:options:)
- 커스텀 URL Sceme에 등록된 url에 관해서 딥링크 처리에 사용
- 단, APNs payload에 커스텀 key로 "deep_link"와 같은 필드로 내려오는 경우 이 메소드 호출 x
- 브레이즈와 같은 솔루션에서 deepLink 값을 형식에 맞게 직접 넣어서 보낼때 이 메소드 호출 o
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]
) -> Bool {
print(url)
}