mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
68 lines
2.2 KiB
Swift
68 lines
2.2 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
class LocalNotificationManager {
|
|
|
|
var notifications = [Notification]()
|
|
|
|
// Step 1 Request Permissions for notifications
|
|
private func requestAuthorization() {
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
|
|
|
|
if granted == true && error == nil {
|
|
self.scheduleNotifications()
|
|
}
|
|
}
|
|
}
|
|
|
|
func schedule() {
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
switch settings.authorizationStatus {
|
|
case .notDetermined:
|
|
self.requestAuthorization()
|
|
case .authorized, .provisional:
|
|
self.scheduleNotifications()
|
|
default:
|
|
break // Do nothing
|
|
}
|
|
}
|
|
}
|
|
|
|
// This function iterates over the Notification objects in the notifications array and schedules them for delivery in the future
|
|
private func scheduleNotifications() {
|
|
for notification in notifications {
|
|
let content = UNMutableNotificationContent()
|
|
content.subtitle = notification.subtitle
|
|
content.title = notification.title
|
|
content.body = notification.content
|
|
content.sound = .default
|
|
content.interruptionLevel = .timeSensitive
|
|
|
|
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
|
|
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
|
|
|
|
UNUserNotificationCenter.current().add(request) { error in
|
|
|
|
guard error == nil else { return }
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check and debug what local notifications have been scheduled
|
|
func listScheduledNotifications() {
|
|
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
|
|
|
|
for notification in notifications {
|
|
print(notification)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct Notification {
|
|
var id: String
|
|
var title: String
|
|
var subtitle: String
|
|
var content: String
|
|
}
|