Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] 2. Push Notification 응용 - Silent Push Notification (사일런트 푸시 방법, 푸시를 이용한 백그라운드에서 업데이트 방법) 본문

iOS 응용 (swift)

[iOS - swift] 2. Push Notification 응용 - Silent Push Notification (사일런트 푸시 방법, 푸시를 이용한 백그라운드에서 업데이트 방법)

jake-kim 2023. 2. 18. 22:23

1. Push Notification 응용 - 테스트 방법 (Pusher, APNs)

2. Push Notification 응용 - Silent Push Notification (사일런트 푸시, 푸시를 이용한 백그라운드에서 업데이트 방법) <

3. Push Notification 응용 - Rich Push Notification (Notification Service Extension, 푸시 내용 변경하여 띄우기)

4. Push Notification 응용 - 시스템 푸시에 이미지 넣기 (Notification Service Extension, mutable-content)

5. Push Notification 응용 - 푸시 커스텀 UI 구현 방법 (Notification Content Extension, category)

6. Push Notification 응용 - 푸시 앱 아이콘 부분 커스텀 방법 (메시지 앱에서의 썸네일 아이콘 푸시 구현, 카톡 푸시 썸네일, INSendMessageIntent)

 

Silent Push Notification 이란?

  • 백그라운드에서 앱 업데이트를 하는 것 (사용자가 느끼지 못하지만 앱의 내용이 바뀌어 있게 하고 싶은 경우 사용)
    • 앱이 foreground / background / 종료된 상태일때도 push가 오면 아래에서 알아볼 didReceiveRemoteNotification가 동작
    • 사용처) 채팅앱을 만들때 특정 메시지가 온 경우 or 앱 내에 특정 내용을 바로 변경하는 경우 사용
  • 백그라운드 알림을 사용하여 업데이트
    • 백그라운드 알림: 경고 표시 x, 사운드 재생 x, 앱 아이콘에 배지 표시 x

푸시 환경 준비

  • 이전 포스팅 글 1번글을 참고하여 푸시 테스트 환경 준비 
    • apple developer에서 keys 생성
    • provisioning profile 생성
    • Xcode push notification 활성화 (background modes, push notifications)
    • Pusher로 테스트

Silent Push 페이로드

  • json 페이로드에 3가지 값이 추가로 있어야 가능
    • conent-available을 1로 설정하면 암묵적으로 silent push를 의미
    • push type을 background로 설정
    • priority를 5로 설정
"content-available": 1,
"apns-push-type": "background",
"apns-priority": 5

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

  • payload json 전체 
    • (다음 포스팅 3번글에서 알아볼 Rich Push는 "mutable-content": 1 사용하므로 구분할 것)
{
    "aps": {
        "content-available": 1,
        "apns-push-type": "background",
        "apns-priority": 5,
    },
    "data": {
    	"title": "iOS 앱 개발 알아가기"
    }
}
  • 이 푸시는 우선순위가 낮으므로 짧은 시간에 push notification 수가 많아지면 전달되지 않을 수 있으므로 시간당 2~3개 이상은 보내지 말 것
    • 또 백그라운드로 작업할 수 있는 시간은 30초로 제한되어 있으므로 30초안으로 작업을 끝낼것

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

  • Silent Push 처리하는 델리게이트 부분
    • AppDelegate의 application(_:didReceiveRemoteNotification:fetchCompletionHandler) 부분
// 사일런트 푸시 처리 메소드
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("Receive silent push>", userInfo)
    completionHandler(.newData)
}

 

(완료) - 시뮬레이터에서는 silent push가 안되므로 실제 기기에서 테스트 필요

silent push를 보내면 사용자가 push를 탭하지 않아도 바로 didReceiveRemoteNotification 델리게이트 호출

번외) UNUserNotificationCenter 

  • 푸시 관련 델리게이트에서 대표적으로 willPresent, didReceive가 있는데 이 용도는 silent push 용도가 아니므로 주의
// AppDelegate.swift

let center = UNUserNotificationCenter.current()
center.delegate = self

...

extension AppDelegate: UNUserNotificationCenterDelegate {
    // foreground에서 시스템 푸시를 수신했을 때 해당 메소드가 호출
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
    }
    
    // foreground, background에서 시스템 푸시를 탭하거나 dismiss했을때 해당 메소드가 호출
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
    }
}

* cf) 이 밖의 푸시 관련 처리 메소드들 개념은 이전 포스팅 글, 푸시 메소드들 총 정리 참고

 

* 전체 코드: https://github.com/JK0369/ExPushTest

* 참고

https://clevertap.com/glossary/silent-push-notifications/

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

Comments