I made two classes to manage local notifications, to get more notification functionality I used local notification delegation, but it's doesn't work (the delegation functions) may be because I didn't set it correctly, if there is any way to fix this issue please share it with me.
Notification Manager
internal final class LocalNotificationManager: ObservableObject {
// MARK: - Properties
@Published var authorizationState: UNAuthorizationStatus?
// MARK: - Methods
func reloadAuthorizationState() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
DispatchQueue.main.async {
self.authorizationState = settings.authorizationStatus
}
}
}
func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
// Do something if user allowing notifications
DispatchQueue.main.async {
self.authorizationState = .authorized
}
} else if !granted {
// Do something if user do not allow the notifications
DispatchQueue.main.async {
self.authorizationState = .denied
}
} else if let error = error {
// Show some message
print(error.localizedDescription)
}
}
}
func schedulNotification(for taskModel: TaskModel) {
// Set Delegation
let delegate = LocalNotificationDelegate()
UNUserNotificationCenter.current().delegate = delegate
let content = UNMutableNotificationContent()
content.interruptionLevel = .timeSensitive
content.title = ""
content.body = taskModel.text
content.subtitle = "\(taskModel.priority != .none ? "\(taskModel.priority.rawValue) Priority" : "")"
content.categoryIdentifier = "Task Actions" // Same Identifier in registerCategories()
content.sound = UNNotificationSound.default
let taskIdentifier = taskModel.id.uuidString
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: taskIdentifier, content: content, trigger: trigger)
registerCategories(for: taskModel)
UNUserNotificationCenter.current().add(request)
}
private func registerCategories(for taskModel: TaskModel) {
let markAsCompleted = UNNotificationAction(identifier: "MARK_AS_COMPLETED", title: "Mark as Completed", icon: UNNotificationActionIcon(systemImageName: "checkmark.circle"))
let remindMeInAnHour = UNNotificationAction(identifier: "REMIND_ME_IN_AN_HOUR", title: "Remind Me in an Hour", icon: UNNotificationActionIcon(systemImageName: "clock"))
let remindMeTomorrow = UNNotificationAction(identifier: "REMIND_ME_TOMORROW", title: "Remind Me Tomorrow", icon: UNNotificationActionIcon(systemImageName: "calendar"))
let placeholder = "Task"
let category = UNNotificationCategory(identifier: "Task Actions", actions: [markAsCompleted, remindMeInAnHour, remindMeTomorrow], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: placeholder) // // Same Identifier in schedulNotification()
UNUserNotificationCenter.current().setNotificationCategories([category])
}
Notification Delegat
internal final class LocalNotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
completionHandler(.banner)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
// the user tapped to unlock
print("User Tapped to Unlock the App")
case "MARK_AS_COMPLETED":
print("MARK_AS_COMPLETED")
break
case "REMIND_ME_IN_AN_HOUR":
print("REMIND_ME_IN_AN_HOUR")
break
case "REMIND_ME_TOMORROW":
print("REMIND_ME_TOMORROW")
break
default:
break
}
completionHandler()
}
}