티스토리 뷰

iOS 10 이전에서는 앱이 켜진 상태에서 푸시 핸들링을 하려면, 직접 만든 커스텀 알러트만 띄울 수 있었다고 한다.

 

iOS 10 이상부터는 앱이 켜졌을 때도 iOS에서 제공하는 푸시 형태로도 띄울 수 있게 되었다. 

 

바로 userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 함수를 이용하면 된다!!

 

 

노티피케이션이 왔을 때 앱이 실행중이면, user notification center에서 이 메소드를 콜해줘서 노티피케이션을 직접 앱으로 쏴준다!.!

 

이 메소드를 구현해두면, 여기서 액션을 처리해주면 된다. 

액션 처리를 다 했으면 컴플리션핸들러 블록을 구현하고 유저한테 알러트를 어떻게 보여줄건지 지정해줘야 한다. 

만약 이 델리게이트를 구현 안해뒀으면, 시스템은 UNNotificationPresentationOptionNone 으로 설정해둔 것 처럼 구현될 것이다. 아예 유저노티피케이션 센터에 델리게이트도 전달해주지 않았으면, 시스템은 notification의 오리지널 옵션으로 유저한테 알람을 줄 것이다. 

 

 

영어 원문

더보기

If your app is in the foreground when a notification arrives, the shared user notification center calls this method to deliver the notification directly to your app.

If you implement this method, you can take whatever actions are necessary to process the notification and update your app.

When you finish, call the completionHandler block and specify how you want the system to alert the user, if at all. 

If your delegate does not implement this method, the system behaves as if you had passed the UNNotificationPresentationOptionNone option to the completionHandlerblock.

If you do not provide a delegate at all for the UNUserNotificationCenter object, the system uses the notification’s original options to alert the user.

 

 

 

badge / sound / alert중에 원하는거 해주면 된다. 

 

 

===코드===

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    /// 앱이 foreground  상태일 때 Push 받으면 alert를 띄워준다
    completionHandler([.alert, .sound])
  }

 

 

?? 근데 푸시 받았는데 이쪽으로 오네 ??

 

-> delegate를 채택해서 해결했다. 

delegate를 채택하지 않으면 동작하지 않는다. delegate 채택 잊지 말자!!

 

 

 

 

 

이렇게 구현해주면 된다. 끝

 

잘못된 부분이나 궁금하신 부분, 보충 설명이 필요하신 점은 댓글로 남겨주세요.

 

[문서]

https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함