-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] local notification
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
KkuMulKum/Resource/Notification/LocalNotificationManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// LocalNotificationManager.swift | ||
// KkuMulKum | ||
// | ||
// Created by 이지훈 on 7/17/24. | ||
// | ||
|
||
import Foundation | ||
import UserNotifications | ||
|
||
class LocalNotificationManager { | ||
|
||
private let notificationCenter = UNUserNotificationCenter.current() | ||
|
||
func requestAuthorization(completion: @escaping (Bool) -> Void) { | ||
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in | ||
completion(granted) | ||
} | ||
} | ||
|
||
func scheduleNotification(title: String, body: String, triggerDate: Date) { | ||
let content = UNMutableNotificationContent() | ||
content.title = title | ||
content.body = body | ||
content.sound = .default | ||
|
||
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: triggerDate) | ||
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) | ||
|
||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) | ||
|
||
notificationCenter.add(request) { error in | ||
if let error = error { | ||
print("Error scheduling notification: \(error)") | ||
} | ||
} | ||
} | ||
} |