Day_Count_Memory_Days/anniversary_Project/Tool/NotificationCenter.swift
2024-07-15 11:52:15 +08:00

34 lines
1000 B
Swift

//
// NotificationCenter.swift
// anniversary_Project
//
// Created by 16 on 2024/7/8.
//
import Foundation
import UserNotifications
//
func scheduleNotification(at hour: Int, minute: Int, identifier: String, title: String, body: String) {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UserNotifications.UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Error adding notification: \(error.localizedDescription)")
}
}
}