Meshtastic-Apple/Meshtastic/Helpers/LocalNotificationManager.swift

108 lines
3.6 KiB
Swift
Raw Normal View History

2021-10-06 17:51:52 -07:00
import Foundation
import SwiftUI
2024-06-03 02:17:55 -07:00
import OSLog
2025-01-22 12:18:53 -08:00
class LocalNotificationManager {
2025-01-22 12:20:43 -08:00
var notifications = [Notification]()
2025-01-22 12:18:53 -08:00
let thumbsUpAction = UNNotificationAction(identifier: "messageNotification.thumbsUpAction", title: "👍 \(Tapbacks.thumbsUp.description)", options: [])
let thumbsDownAction = UNNotificationAction(identifier: "messageNotification.thumbsDownAction", title: "👎 \(Tapbacks.thumbsDown.description)", options: [])
2025-02-15 10:46:35 -08:00
let replyInputAction = UNTextInputNotificationAction(identifier: "messageNotification.replyInputAction", title: "Reply".localized, options: [])
2021-11-29 15:59:06 -08:00
2025-01-22 12:20:43 -08:00
// Step 1 Request Permissions for notifications
private func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
2025-01-22 12:20:43 -08:00
if granted == true && error == nil {
2025-01-22 12:18:53 -08:00
self.scheduleNotifications()
2025-01-22 12:20:43 -08:00
}
}
}
2021-11-29 15:59:06 -08:00
2025-01-22 12:18:53 -08:00
func schedule() {
2025-01-22 12:20:43 -08:00
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined:
2025-01-22 12:18:53 -08:00
self.requestAuthorization()
2025-01-22 12:20:43 -08:00
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() {
2025-01-22 12:18:53 -08:00
let messageNotificationCategory = UNNotificationCategory(
identifier: "messageNotificationCategory",
actions: [thumbsUpAction, thumbsDownAction, replyInputAction],
intentIdentifiers: [],
options: .customDismissAction
)
2025-01-22 12:18:53 -08:00
UNUserNotificationCenter.current().setNotificationCategories([messageNotificationCategory])
2025-01-22 12:20:43 -08:00
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
2025-01-22 12:18:53 -08:00
if notification.target != nil {
content.userInfo["target"] = notification.target
}
2025-01-22 12:18:53 -08:00
if notification.path != nil {
content.userInfo["path"] = notification.path
}
2025-01-22 12:18:53 -08:00
if notification.messageId != nil {
content.categoryIdentifier = "messageNotificationCategory"
content.userInfo["messageId"] = notification.messageId
}
2025-01-22 12:18:53 -08:00
if notification.channel != nil {
content.userInfo["channel"] = notification.channel
}
2025-01-22 12:18:53 -08:00
if notification.userNum != nil {
content.userInfo["userNum"] = notification.userNum
}
2025-01-22 12:20:43 -08:00
if notification.critical {
content.sound = UNNotificationSound.defaultCritical
}
2025-01-22 12:20:43 -08:00
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: nil)
2025-01-22 12:20:43 -08:00
UNUserNotificationCenter.current().add(request) { error in
2025-01-22 12:18:53 -08:00
if let error {
2025-03-31 22:06:00 -07:00
Logger.services.error("Error Scheduling Notification: \(error.localizedDescription, privacy: .public)")
}
2025-01-22 12:20:43 -08:00
}
}
}
2025-01-22 12:20:43 -08:00
// Check and debug what local notifications have been scheduled
func listScheduledNotifications() {
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
2025-01-22 12:20:43 -08:00
for notification in notifications {
2025-01-22 12:18:53 -08:00
Logger.services.debug("\(notification, privacy: .public)")
2025-01-22 12:20:43 -08:00
}
}
}
}
2025-01-22 12:18:53 -08:00
struct Notification {
2025-01-22 12:20:43 -08:00
var id: String
var title: String
var subtitle: String
var content: String
2025-01-22 12:18:53 -08:00
var target: String?
var path: String?
var messageId: Int64?
var channel: Int32?
var userNum: Int64?
2025-01-22 12:20:43 -08:00
var critical: Bool = false
}