[swift] Swift Presentr를 활용한 푸시 알림 처리 방법

이번에는 Swift Presentr 라이브러리를 활용하여 앱에서 푸시 알림을 처리하는 방법에 대해 알아보겠습니다.

1. Presentr 라이브러리 설치하기

먼저, Presentr 라이브러리를 설치해야 합니다. Podfile에 다음과 같은 내용을 추가합니다.

pod 'Presentr'

그리고 터미널에서 다음 명령을 실행하여 라이브러리를 설치합니다.

$ pod install

2. PresentrDelegate 구현하기

PresentrDelegate 프로토콜을 구현하여 푸시 알림을 처리하는 뷰 컨트롤러에서 Presentr을 초기화하고 알림창을 표시할 수 있습니다. 다음은 PresentrDelegate의 구현 예시입니다.

import Presentr

class PushNotificationViewController: UIViewController, PresentrDelegate {
    
    let presenter: Presentr = {
        let width = ModalSize.full
        let height = ModalSize.full
        let center = ModalCenterPosition.customOrigin(origin: CGPoint(x: 0, y: 0))
        let customType = PresentationType.custom(width: width, height: height, center: center)
        let customPresenter = Presentr(presentationType: customType)
        customPresenter.transitionType = .coverHorizontalFromRight
        customPresenter.dismissTransitionType = .coverHorizontalFromLeft
        customPresenter.dismissOnSwipe = true
        return customPresenter
    }()

    func showPushNotification() {
        let alertViewController = UIAlertController(title: "푸시 알림", message: "새로운 알림이 도착했습니다.", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "확인", style: .default, handler: nil)
        alertViewController.addAction(okAction)
        
        customPresentViewController(presenter, viewController: alertViewController, animated: true, completion: nil)
    }
}

위의 코드에서는 Presentr 객체를 생성한 뒤, 원하는 알림 형식과 애니메이션을 설정하고, Presentr을 사용하여 UIAlertController를 표시하는 방법을 보여줍니다.

3. 푸시 알림 호출하기

푸시 알림을 받은 경우, 해당 뷰 컨트롤러에서 PresentrDelegate의 showPushNotification() 메서드를 호출하면 알림창이 표시됩니다.

let pushNotificationVC = PushNotificationViewController()
pushNotificationVC.showPushNotification()

위의 코드는 푸시 알림 뷰 컨트롤러를 생성한 후, showPushNotification() 메서드를 호출하여 알림창을 표시하는 예시입니다.

마치며

이제 Swift Presentr를 이용하여 앱에서 푸시 알림을 처리하는 방법을 알아보았습니다. Presentr 라이브러리를 사용하면 간편하게 다양한 알림창을 만들 수 있습니다. Presentr의 다양한 옵션을 활용하여 앱에 적합한 푸시 알림 처리를 구현해보세요.