diff --git a/Meshtastic Client.xcodeproj/project.pbxproj b/Meshtastic Client.xcodeproj/project.pbxproj index 85023f92..14b5072a 100644 --- a/Meshtastic Client.xcodeproj/project.pbxproj +++ b/Meshtastic Client.xcodeproj/project.pbxproj @@ -659,7 +659,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.23; + MARKETING_VERSION = 1.24; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; @@ -686,7 +686,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.23; + MARKETING_VERSION = 1.24; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index 6098084e..4aea97b4 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -177,9 +177,8 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } else { - connectToDevice(id: lastConnectedNode) + success = false } - } return success } @@ -423,6 +422,13 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph MessageModel(messageId: decodedInfo.packet.id, messageTimeStamp: decodedInfo.packet.rxTime, fromUserId: decodedInfo.packet.from, toUserId: decodedInfo.packet.to, fromUserLongName: fromUser?.user.longName ?? "Unknown", toUserLongName: toUserLongName, fromUserShortName: fromUser?.user.shortName ?? "???", toUserShortName: toUserShortName, receivedACK: decodedInfo.packet.decoded.wantResponse, messagePayload: messageText, direction: "IN")) messageData.save() + let manager = LocalNotificationManager() + + manager.notifications = [ + Notification(id: ("notifiction.id.\(decodedInfo.packet.id)"), title: ("\(fromUser?.user.shortName ?? "???") says \(messageText))"))] + manager.schedule() + manager.listScheduledNotifications() + } else { print("not a valid UTF-8 sequence") } diff --git a/MeshtasticClient/Helpers/LocalNotificationManager.swift b/MeshtasticClient/Helpers/LocalNotificationManager.swift index ab4adfd2..54f795b5 100644 --- a/MeshtasticClient/Helpers/LocalNotificationManager.swift +++ b/MeshtasticClient/Helpers/LocalNotificationManager.swift @@ -1,8 +1,73 @@ -// -// LocalNotificationManager.swift -// MeshtasticClient -// -// Created by Garth Vander Houwen on 10/6/21. -// - 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.title = notification.title + content.sound = .default + + 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 } + + print("Notification scheduled! --- ID = \(notification.id)") + } + } + } + + // 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 +} diff --git a/MeshtasticClient/Info.plist b/MeshtasticClient/Info.plist index de26c678..1b1c689f 100644 --- a/MeshtasticClient/Info.plist +++ b/MeshtasticClient/Info.plist @@ -41,6 +41,10 @@ UIApplicationSupportsIndirectInputEvents + UIBackgroundModes + + bluetooth-central + UILaunchScreen UIRequiredDeviceCapabilities