IOS
Swift 로컬에서 Notification(알림) 구현하기.
일용직 코딩노동자
2021. 12. 6. 17:49
728x90
반응형
import UserNotifications
우선 이렇게 임포트 하나 해주시구요.
let userNotificationCenter = UNUserNotificationCenter.current()
클래스 딴 전역으로 하나 선언해줍니다.
func callNotification(seconds: Double, title : String, body : String) {
let notificationContent = UNMutableNotificationContent()
notificationContent.title = title
notificationContent.body = body
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: seconds, repeats: false)
let request = UNNotificationRequest(identifier: "Notification",
content: notificationContent,
trigger: trigger)
userNotificationCenter.add(request) { error in
if let error = error {
print("Error: ", error)
}
}
}
이렇게 함수를 셋팅하시고...
사용 하 실 때는 원하시는 위치에서
callNotification(1,"알 림","푸시 알림 테스트")
이렇게 사용해 주시면 됩니다.
푸시가 발동되는 시간간격은 해당 앞의 더블형 seconds변수를 조절해주시면 됩니다.
728x90
반응형