From efa34110c2b7f2daebb83086b61417334296ba4d Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Mon, 21 Oct 2024 14:08:05 +0200 Subject: [PATCH] Display chat notification when app is on foreground if the message comes from elsewhere that the currently displayed chatroom --- Linphone/LinphoneApp.swift | 14 ++++++++++++++ Linphone/UI/Main/ContentView.swift | 6 +++--- .../Fragments/ConversationFragment.swift | 12 ++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Linphone/LinphoneApp.swift b/Linphone/LinphoneApp.swift index 9ecfffa09..487346789 100644 --- a/Linphone/LinphoneApp.swift +++ b/Linphone/LinphoneApp.swift @@ -22,6 +22,7 @@ import linphonesw import UserNotifications let accountTokenNotification = Notification.Name("AccountCreationTokenReceived") +var displayedChatroomPeerAddr: String? class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate { @@ -81,6 +82,19 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele completionHandler() } + // Display notifications on foreground + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + let userInfo = notification.request.content.userInfo + Log.info("Received push notification in foreground, payload= \(userInfo)") + + if let callId = userInfo["CallId"] as? String, let peerAddr = userInfo["peer_addr"] as? String, let localAddr = userInfo["local_addr"] as? String { + // Only display notification if we're not in the chatroom they come from + if displayedChatroomPeerAddr != peerAddr { + completionHandler([.banner, .sound]) + } + } + } + func applicationWillTerminate(_ application: UIApplication) { Log.info("IOS applicationWillTerminate") CoreContext.shared.doOnCoreQueue(synchronous: true) { core in diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index 776f6f0bd..6a11ce794 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -1242,9 +1242,9 @@ struct ContentView: View { } class NavigationManager: ObservableObject { - @Published var selectedCallId: String? = nil - @Published var peerAddr: String? = nil - @Published var localAddr: String? = nil + @Published var selectedCallId: String? + @Published var peerAddr: String? + @Published var localAddr: String? func openChatRoom(callId: String, peerAddr: String, localAddr: String) { self.selectedCallId = callId diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift index 4a60f942e..a61fd9186 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift @@ -69,7 +69,13 @@ struct ConversationFragment: View { .onRotate { newOrientation in orientation = newOrientation } + .onAppear() { + displayedChatroomPeerAddr = conversationViewModel.displayedConversation?.remoteSipUri + Log.info("debugtrace = onAppear: displayedChatroomPeerAddr = \(displayedChatroomPeerAddr)") + } .onDisappear { + displayedChatroomPeerAddr = nil + Log.info("debugtrace = onDisappear: displayedChatroomPeerAddr = nil") conversationViewModel.removeConversationDelegate() } .sheet(isPresented: $conversationViewModel.isShowSelectedMessageToDisplayDetails, onDismiss: { @@ -109,7 +115,13 @@ struct ConversationFragment: View { .onRotate { newOrientation in orientation = newOrientation } + .onAppear() { + displayedChatroomPeerAddr = conversationViewModel.displayedConversation?.remoteSipUri + Log.info("debugtrace = onAppear: displayedChatroomPeerAddr = \(displayedChatroomPeerAddr)") + } .onDisappear { + displayedChatroomPeerAddr = nil + Log.info("debugtrace = onDisappear: displayedChatroomPeerAddr = nil") conversationViewModel.removeConversationDelegate() } .halfSheet(showSheet: $conversationViewModel.isShowSelectedMessageToDisplayDetails) {