Initial Commit
This commit is contained in:
44
Screaming Reminder/Utils/NotificationDelegate.swift
Normal file
44
Screaming Reminder/Utils/NotificationDelegate.swift
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// NotificationDelegate.swift
|
||||
// Screaming Reminder
|
||||
//
|
||||
// Created by Amine Bou on 12/07/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
class NotificationDelegate: NSObject , UNUserNotificationCenterDelegate{
|
||||
|
||||
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
||||
didReceive response: UNNotificationResponse,
|
||||
withCompletionHandler completionHandler:
|
||||
@escaping () -> Void) {
|
||||
|
||||
let title = response.notification.request.content.title
|
||||
switch response.actionIdentifier {
|
||||
case "DONE_ACTION":
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = "Bravo !"
|
||||
content.subtitle = "Tu as fais ce que tu devais faire ! (Il faudra ignorer les autres notifications 😅)"
|
||||
content.sound = UNNotificationSound.default
|
||||
|
||||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
|
||||
|
||||
// choose a random identifier
|
||||
let request = UNNotificationRequest(identifier: "\(title)-done", content: content, trigger: trigger)
|
||||
|
||||
// add our reminder request
|
||||
UNUserNotificationCenter.current().add(request)
|
||||
|
||||
break
|
||||
default:
|
||||
exit(0)
|
||||
break
|
||||
}
|
||||
|
||||
// Always call the completion handler when done.
|
||||
completionHandler()
|
||||
}
|
||||
}
|
35
Screaming Reminder/Utils/NotificationsUtils.swift
Normal file
35
Screaming Reminder/Utils/NotificationsUtils.swift
Normal file
@ -0,0 +1,35 @@
|
||||
//
|
||||
// NotificationsUtils.swift
|
||||
// Screaming Reminder
|
||||
//
|
||||
// Created by Amine Bou on 12/07/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
|
||||
func scheduleNotifications(reminder: Reminder) {
|
||||
let notification = Notification(reminder: reminder)
|
||||
notification.triggers.forEach {
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = notification.reminder.label
|
||||
content.sound = UNNotificationSound.defaultCritical
|
||||
content.categoryIdentifier = "CAT"
|
||||
|
||||
let request = UNNotificationRequest(identifier: getIdentifier(reminder: reminder, trigger: $0), content: content, trigger: $0)
|
||||
|
||||
// add our reminder request
|
||||
UNUserNotificationCenter.current().add(request)
|
||||
}
|
||||
}
|
||||
|
||||
func cancelNotifications(reminder: Reminder) {
|
||||
let identifiers = getTriggersForReminder(reminder: reminder).map {
|
||||
getIdentifier(reminder: reminder, trigger: $0)
|
||||
}
|
||||
|
||||
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
|
||||
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
|
||||
}
|
Reference in New Issue
Block a user