From b773df1204755fb53676ff3d2939b9da8471b543 Mon Sep 17 00:00:00 2001 From: Swift Date: Fri, 24 Mar 2023 18:45:35 -0400 Subject: [PATCH] Local Zap Notifications Changelog-Added: Local zap notifications --- damus/ContentView.swift | 1 - damus/Models/HomeModel.swift | 31 ++++++++++++++++++++++++++++--- damus/damusApp.swift | 14 ++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/damus/ContentView.swift b/damus/ContentView.swift index f21a653c..a7661b27 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -776,7 +776,6 @@ func setup_notifications() { } } - func find_event(state: DamusState, evid: String, search_type: SearchType, find_from: [String]?, callback: @escaping (NostrEvent?) -> ()) { if let ev = state.events.lookup(evid) { callback(ev) diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index ee897ea5..f652b539 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -145,9 +145,13 @@ class HomeModel: ObservableObject { return } - if handle_last_event(ev: ev, timeline: .notifications) && damus_state.settings.zap_vibration { - // Generate zap vibration - zap_vibrate(zap_amount: zap.invoice.amount) + if handle_last_event(ev: ev, timeline: .notifications) { + if damus_state.settings.zap_vibration { + // Generate zap vibration + zap_vibrate(zap_amount: zap.invoice.amount) + } + // Create in-app local notification for zap received. + create_in_app_zap_notification(zap_amount: zap.invoice.amount) } return @@ -928,3 +932,24 @@ func zap_vibrate(zap_amount: Int64) { vibration_generator.impactOccurred() } +func create_in_app_zap_notification(zap_amount: Int64) { + let sats = zap_amount / 1000 + let content = UNMutableNotificationContent() + content.title = "Zap" + let satString = sats == 1 ? "sat" : "sats" + content.body = "You have just received \(sats) \(satString)" + content.sound = UNNotificationSound.default + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) + + let request = UNNotificationRequest(identifier: "myZapNotification", content: content, trigger: trigger) + + UNUserNotificationCenter.current().add(request) { error in + if let error = error { + print("Error: \(error)") + } else { + print("Local notification scheduled") + } + } +} + diff --git a/damus/damusApp.swift b/damus/damusApp.swift index f9486eba..e7193de7 100644 --- a/damus/damusApp.swift +++ b/damus/damusApp.swift @@ -9,6 +9,7 @@ import SwiftUI @main struct damusApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { MainView() @@ -43,6 +44,19 @@ struct MainView: View { } } +class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { + UNUserNotificationCenter.current().delegate = self + return true + } + + // Handle the notification in the foreground state + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + // Display the notification in the foreground + completionHandler([.alert, .sound, .badge]) + } +} + func needs_setup() -> Keypair? { return get_saved_keypair() }