[Swift] - Local Notification

sai06266·2024년 3월 6일

Swift

목록 보기
10/10

사용자에게 알림을 보낼 때 사용
-> 권한이 있는 사용자에게만 가능

User Notification

  • 사용자 대상 알림을 앱 로컬에서 생성하거나 서버에서 사용자 기기로 푸시
  • 소리/문구/이미지/아이콘/뱃지/푸시 배너 등으로 다양하게 알림
  • Notifiction을 동작 시키기 위해 Request와 Trigger를 이용
  • Local Notification과 Remote Notification으로 분류
  • NotificationCenter와는 용도가 다름

로컬 알림은 UNNotificationRequest 객체를 만들어 생성됩니다.
이 객체에는 알림의 컨텐츠 및 예약된 시간 등의 정보가 포함됩니다.

import UIKit
import UserNotifications

let center = UNUserNotificationCenter.current()

// 알림의 콘텐츠 설정
let content = UNMutableNotificationContent()
content.title = "알림 제목"
content.body = "알림 내용"
content.sound = UNNotificationSound.default

// 알림 발송 시각 설정 (5초 후)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

// 알림 요청 생성
let request = UNNotificationRequest(identifier: "testNotification", content: content, trigger: trigger)

// 알림을 스케줄링
center.add(request) { (error) in
    if let error = error {
        print("로컬 알림을 스케줄링하는 동안 오류 발생: \(error)")
    }
}

0개의 댓글