티스토리 뷰

반응형

# notification을 눌렀을 때 들어오는 메소드

1) application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

- remote notification을 눌렀을 때 들어오는 메소드 

2) userNotificationCenter(_:didReceive:withCompletionHandler:)

- remote, local notification을 눌렀을 때 들어오는 메소드 
- iOS 10 이상부터 remote notification을 이 메소드로 처리하라고 권장함.

- Handling Notifications and Notification-Related Actions 문서에서는 항상 competionHandler() 를 콜하라고 적혀있음 (테스트 해보면 이 코드가 있고 없고 차이는 없긴 함..!)

 

 

 

 ⚠️ 주의사항 ⚠️

 

1. application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 

2. userNotificationCenter(_:didReceive:withCompletionHandler:)

 

2번 없이 1번 메소드만 있을 때, remote notification을 누르면 1번이 잘 불림

하지만 두 메소드가 같이 있을 때, remote notification을 누르면 오직  2번 메소드만 불림!

(iOS 14.4에서 테스트해봄)

 

✔️ 결론 ✔️

iOS Deployment Target이 10이상이라면 

userNotificationCenter(_:didReceive:withCompletionHandler:) 이 메소드 하나만 쓰고

이 안에서 local, remote 알림을 분기해서 특정 화면으로 랜딩하는 등의 작업을 해줘야함.

 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        let identifier = response.notification.request.identifier
        
        // remote, local notification 구분 후, do something!
        ...
        completionHandler()
    }

 

 



 

 

 

Reference 

 

https://stackoverflow.com/questions/46758030/in-which-method-should-i-handle-ios-remote-notification

 

In which method should I handle iOS remote notification?

I know similar questions have been asked many times. But it is still very confusing to me after reading those threads, especially after UNUserNotificationCenter is introduced in iOS 10. The official

stackoverflow.com

 

https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10

 

didReceiveRemoteNotification not called, iOS 10

In iOS 9.3, the didReceiveRemoteNotification method gets called on both of the following occasions. 1) When the push notification is received 2) When the user launches the app by tapping on the

stackoverflow.com

 

반응형
댓글