-
로컬 푸시 알림 기능 구현 (Local Notification)ios 2020. 12. 17. 16:22
Questions before studying
- 푸시 알림을 서버에서 보내는 것 말고 로컬에서도 할 수 있는지
- 가능 하다면 , 앱이 suspended나 background에서도 가능 하는지
Answer For Questions
If the delivery of the notification occurs when your app is not running or in the background, the system interacts with the user for you. If your app is in the foreground, the system delivers the notification to your app for handling
→ 앱이 백그라운드나 포그라운드가 아닐 경우에도 노티피케이션이 오는 경우는 시스템이 사용자와 상호작용하기 떄문이다.
→ 만약 앱이 포그라운드 상태일 경우에는 , 시스템은 앱에게 핸들링을 위해 노티피케이션을 앱에 전달한다.
Required
→ import UserNotifications
→ UNMutableNotificationContent
→ Scheduling a Notification Locally from Your App (article)
What is a UNMutableNotificationContent?
- 일정한 시간을 설정하여 로컬로 노티피케이션을 보낼 수 있는 오브젝트
- 이 오브젝트는 Aler의 타이틀과 메세지 , 알림음 , 앱 뱃지를 설정할 때 사용하면 된다.
What is a UNNotificationRequest?
Create a UNNotificationRequest object when you want to schedule the delivery of a local notification. A notification request object contains a UNNotificationContent object with the payload to be delivered, and it contains the UNNotificationTrigger object with the conditions that trigger the delivery of the notification. To schedule the delivery of your notification, pass your request object to the add(_:withCompletionHandler:) method of the shared user notification center object.
→ local notification을 스케줄링 하기 위해 , UNNotificationRequest object를 생성하야한다. notification request object는 페이로드와 함꼐 전달된 UNNotificationContent object를 포함한다. 그리고 delivery of notification을 조건과 함께 발생시키는 UNNotificationTrigger object도 포함한다. delivery of notification을 설정하기 위해서 해당 request object를 user notification center object에 add(_:withCompletionHandler:)에 전달해줘야 합니다.
How does it work?
-
Create The Notification's Content
- properties of a UNMutableNotificationContent object
- properties
- title
- subTitle
- body
- badge
- sound
- launchImageName
- userInfo
- attachments
- properties
- properties of a UNMutableNotificationContent object
-
Specify The Conditions for Delivery
- Specify the conditions for delivering your notification using
- UNCalendarNotificationTrigger
- UNTimeIntervalNotificationTrigger
- UNLocationNotificationTrigger
- Specify the conditions for delivering your notification using
-
Create and Register a Notification Request
- Create a UNNotificationRequest object that includes your content and trigger conditions
- Call the add(_:withCompletionHandler:) method to schedule your request with the system
How to remove when push options off
-
removePendingNotificationRequests(withIdentifiers:)
This method executes asynchronously, removing the pending notification requests on a secondary thread.
- 해당 매서드는 비동기 방식으로 실행됩니다. 서브 스레드에서 보류중인 notification request를 제거 합니다.
정리하자면 , Local Notification을 보내기 위해서는 3 단계가 필요합니다.
첫 번째 , 알림에 보여줄 컨텐츠 , 뱃지 아이콘 , 알림음 등을 설정 합니다.
두 번쨰 , Notifcation을 전송하기 위해 조건을 설정해줍니다.
총 3가지의 조건들(날짜기반 알림 , 시간기반 알림 , 위치기반 알림) 중 각 상황에 맞게 사용하시면 되겠습니다.
세번쨰 , 알람 컨텐츠가 담긴 객체 UNMutableNotificationContent , 실행 조건 설정이 담긴 객체
UNNotificationTrigger를 UNNotificationRequest 객체에 담은 후 , UNUserNotificationCenter 인스턴스에 add() 매서드를 사용하여
추가해주시면 되겠습니다.
그리고 해당 Request를 제거하시려면, removePendingNotificationRequests(id)를 사용하여 해당 reqeust를 보조 스레드에서 제거할 수 있습니다.
아래는 제가 구현한 코드 입니다.
[Notification 구현부]
[Notification 시간 설정]
'ios' 카테고리의 다른 글
MVP 패턴에 대하여 (0) 2022.02.21 custom font 적용 (0) 2021.10.25 ARC: Resolving Strong Reference Cycles Between Class Instances (0) 2020.09.07 ARC (0) 2020.09.06 네트워크 통신 모듈화 with Alamofire (0) 2020.07.21