푸시 알림 데이터 이용
푸시 알림 메시지가 수신되거나 사용자에 의해 탭될 때 호출되는 메쏘드와 해당 상황에서 데이터를 추출하여 이용하는 방법에 대해 안내합니다.
앱이 포그라운드 상태일 때 화면에 알림 메시지가 보여지도록 구현하지 않은 이상 화면에 배너 형태로 알림 메시지가 보이지 않습니다.
앱이 포그라운드 상태인데 화면에 배너 형태로 알림 메시지가 보인다면 이미 고객님께서 알림 메시지가 보여지도록 구현한 것입니다. 이 경우, 해당 구현부분 상단에 아래의 내용을 참조하여 수정해주셔야 합니다.
알림 메시지가 보이지 않거나 Delegation 메쏘드가 구현되지 않은 경우에는 아래 내용 참고하셔서 해당 메쏘드를 추가하여 데이터를 추출할 수 있습니다.
딥링크 데이터를 이용할 때 아래와 같은 점에 유의해주세요.
- 1.앱이 Foreground상태일 때 딥링크 URL로 화면이 바로 이동하면 이용자가 당황스러울 수 있습니다. 이에 대해 기획단계부터 어떻게 할 것인지 고민해주세요. 보통은 로컬 노티피케이션을 한 번 더 띄우기도 합니다.
- 2.앱의 세 가지 상태 (Foreground, Background, Killed)에 따라 작동 매카니즘이 다릅니다. 각 상태별로 호출 구조를 체크해주세요.
위에서 딥링크와 관련해서 안내드렸는데요, 푸시를 통해 전달되는 데이터를 이용하는 것에 모두 해당되는 내용입니다.
AppDelegate 의 "userNotificationCenter(_:willPresent:withCompletionHandler:):" 메쏘드가 호출됩니다.
Swift
Objective-C
// UNUserNotificationCenterDelegate 프로토콜 구현
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("userInfo: \(notification.request.content.userInfo)")
if let rootDict = notification.request.content.userInfo as NSDictionary? {
if let jsonDict = rootDict["RW_push_payload_WP"] as? NSDictionary {
let title = jsonDict["RW_push_payload_TT"]
let body = jsonDict["RW_push_payload_BD"]
let deepLink = jsonDict["RW_push_payload_deeplink"]
print("Notification title: \(title), body: \(body), deepLink: \(deepLink)")
}
}
completionHandler([.sound, .badge])
}
...
}
// 앱 포그라운드 상태일 때 알림
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)) {
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"푸시 데이터 : %@", userInfo);
NSDictionary *jsonDict = [dictionary objectForKey:@"RW_push_payload_WP"];
if (jsonDict != nil) {
String *title = [jsonDict objectForKey:@"RW_push_payload_TT"];
String *body = [jsonDict objectForKey:@"RW_push_payload_BD"];
String *deepLink = [jsonDict objectForKey:@"RW_push_payload_deeplink"];
NSLog("Notification title: %@, body: %@, deepLink: %@", title, body, deepLink);
}
completionHandler(UNNotificationPresentationOptionNone); // 포그라운드 상태에서 푸시왔을 때 푸시 마노출
// completionHandler(UNNotificationPresentationOptionAlert); // 포그라운드 상태에서 푸시 왔을 때 푸시 노출
}
jsonDict 는 일반 문자열입니다. 해당 문자열은 JSON 형식으로 되어있으니 사용하시는 JSON Parser를 이용하여 JSON 객체로 전환하여 사용해주세요.
Payload 키 | Payload 값 | 필수여부 |
---|---|---|
RW_push_payload_TT | 메시지 타이틀 | 필수 |
RW_push_payload_BD | 메시지 내용 | 필수 |
RW_push_payload_IM | 이미지 URL | X |
RW_push_payload_MV | 유튜브 동영상 URL | X |
RW_push_payload_deeplink | 딥링크 URL | X |
앱이 백그라운드이거나 Killed 된 상태인 경우, 위의 메쏘드가 호출되지 않습니다.
알림 메시지는 앱이 백그라운드 상태에서 알림 표시줄에 배너 형태 등으로 표시됩니다. 해당 알림 메시지를 탭하면 아래와 같은 "userNotificationCenter(_:didReceive:withCompletionHandler:)" 메쏘드가 호출됩니다. 해당 메쏘드에서 데이터를 추출하여 사용합니다.
Swift
Objective-C
// UNUserNotificationCenterDelegate 프로토콜 구현
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("userInfo: \(response.notification.request.content.userInfo)")
if let rootDict = response.notification.request.content.userInfo as NSDictionary? {
if let jsonDict = rootDict["RW_push_payload_WP"] as? NSDictionary {
let title = jsonDict["RW_push_payload_TT"]
let body = jsonDict["RW_push_payload_BD"]
let deepLink = jsonDict["RW_push_payload_deeplink"]
print("Notification title: \(title), body: \(body), deepLink: \(deepLink)")
}
}
completionHandler()
}
...
}
// 앱 백라운드 상태일 때 알림
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(ios(10.0)){
NSDictionary *userInfo = response.notification.request.content.userInfo;
NSLog(@"푸시 데이터 : %@", userInfo);
NSDictionary *jsonDict = [dictionary objectForKey:@"RW_push_payload_WP"];
if (jsonDict != nil) {
String *title = [jsonDict objectForKey:@"RW_push_payload_TT"];
String *body = [jsonDict objectForKey:@"RW_push_payload_BD"];
String *deepLink = [jsonDict objectForKey:@"RW_push_payload_deeplink"];
NSLog("Notification title: %@, body: %@, deepLink: %@", title, body, deepLink);
}
completionHandler();
}
Last modified 4mo ago