Meshtastic-Apple/Meshtastic/Helpers/LocalNotificationManager.swift

105 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
class LocalNotificationManager {
var notifications = [Notification]()
2024-10-05 15:50:57 -07:00
let thumbsUpAction = UNNotificationAction(identifier: "messageNotification.thumbsUpAction", title: "👍 \(Tapbacks.thumbsUp.description)", options: [])
let thumbsDownAction = UNNotificationAction(identifier: "messageNotification.thumbsDownAction", title: "👎 \(Tapbacks.thumbsDown.description)", options: [])
let replyInputAction = UNTextInputNotificationAction(identifier: "messageNotification.replyInputAction", title: "reply".localized, options: [])
2021-11-29 15:59:06 -08:00
// Step 1 Request Permissions for notifications
2021-11-29 15:59:06 -08:00
private func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted == true && error == nil {
self.scheduleNotifications()
}
}
}
2021-11-29 15:59:06 -08:00
func schedule() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined:
self.requestAuthorization()
case .authorized, .provisional:
self.scheduleNotifications()
default:
break // Do nothing
}
}
}
2021-11-29 15:59:06 -08:00
// This function iterates over the Notification objects in the notifications array and schedules them for delivery in the future
2021-11-29 15:59:06 -08:00
private func scheduleNotifications() {
2024-09-03 22:50:08 -07:00
let messageNotificationCategory = UNNotificationCategory(
identifier: "messageNotificationCategory",
2024-10-05 15:50:57 -07:00
actions: [thumbsUpAction, thumbsDownAction, replyInputAction],
2024-09-03 22:50:08 -07:00
intentIdentifiers: [],
options: .customDismissAction
)
UNUserNotificationCenter.current().setNotificationCategories([messageNotificationCategory])
2024-10-05 15:50:57 -07:00
2021-11-29 15:59:06 -08:00
for notification in notifications {
2024-01-20 14:05:29 -08:00
let content = UNMutableNotificationContent()
content.subtitle = notification.subtitle
content.title = notification.title
content.body = notification.content
content.sound = .default
content.interruptionLevel = .timeSensitive
if notification.target != nil {
content.userInfo["target"] = notification.target
}
2024-01-20 14:05:29 -08:00
if notification.path != nil {
content.userInfo["path"] = notification.path
2024-01-20 14:05:29 -08:00
}
2024-09-03 22:50:08 -07:00
if notification.messageId != nil {
content.categoryIdentifier = "messageNotificationCategory"
content.userInfo["messageId"] = notification.messageId
}
if notification.channel != nil {
content.userInfo["channel"] = notification.channel
}
if notification.userNum != nil {
content.userInfo["userNum"] = notification.userNum
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error {
Logger.services.error("Error Scheduling Notification: \(error.localizedDescription)")
}
}
}
}
2021-11-29 15:59:06 -08:00
// Check and debug what local notifications have been scheduled
2021-11-29 15:59:06 -08:00
func listScheduledNotifications() {
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
for notification in notifications {
2024-06-23 16:11:02 -07:00
Logger.services.debug("\(notification, privacy: .public)")
}
}
}
2021-11-29 15:59:06 -08:00
}
struct Notification {
var id: String
var title: String
var subtitle: String
var content: String
var target: String?
2024-01-20 14:05:29 -08:00
var path: String?
2024-09-03 22:50:08 -07:00
var messageId: Int64?
var channel: Int32?
var userNum: Int64?
}