dev._.note

[TIL] 240215_TIL 본문

TIL (Today I Learned)

[TIL] 240215_TIL

Laena 2024. 2. 15. 11:14

푸시 알람 기능

알람기능 설정이 어려워서 애를 많이 먹었는데 알고 보니 대리자 설정을 안 해주어서 그런 거였다.

AppDelegate에서 대리자 설정을 안 해주어 안 뜨는 문제를 해결했다.

 

AppDelegate에서 UNUserNotificationCenter의 대리자(delegate)를 자신으로 설정

UNUserNotificationCenter.current().delegate = self

 

실제로 적용한 코드

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // UIWindow 생성 및 초기 ViewController 설정
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = CustomTabBarController() // 탭바 CustomTabBarController
        window?.makeKeyAndVisible()
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            if granted {
                print("Notification permission granted.")
            } else {
                print("Notification permission denied.")
            }
        }
        return true
    }

 

AppDelegate에서 대리자를 설정해 주고 알람을 적용해 보면 이렇게 허용창이 뜬다.

 

푸시 알람이 적용된 모습

 

앱 외부에서도 푸시 알람이 잘 뜬다

 

푸시알람을 클릭하면 앱화면으로 들어가고 얼럿창이 뜬다.

'TIL (Today I Learned)' 카테고리의 다른 글

[Swift] XCTest란?  (0) 2024.03.05
[TIL] 240216_TIL  (0) 2024.02.16
[TIL] 240208_TIL  (0) 2024.02.09
[TIL] 240103_TIL  (1) 2024.01.03
[TIL] 231220_TIL  (0) 2023.12.20