From 9901340ea443bb11df86c2d5973ae4bca588be10 Mon Sep 17 00:00:00 2001 From: Saeed Bashir Date: Mon, 8 Jul 2024 15:25:41 +0500 Subject: [PATCH] chore: fix double notification routing and segment callback (#473) * chore: fix double notification routing and segment callback * refactor: address review feedback * refactor: address review feedback --- .../Listeners/BrazeListener.swift | 6 ++++++ .../Providers/BrazeProvider.swift | 5 ++++- .../PushNotificationsManager.swift | 10 +++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/OpenEdX/Managers/PushNotificationsManager/Listeners/BrazeListener.swift b/OpenEdX/Managers/PushNotificationsManager/Listeners/BrazeListener.swift index b117843fc..0a78688e4 100644 --- a/OpenEdX/Managers/PushNotificationsManager/Listeners/BrazeListener.swift +++ b/OpenEdX/Managers/PushNotificationsManager/Listeners/BrazeListener.swift @@ -6,6 +6,7 @@ // import Foundation +import Swinject class BrazeListener: PushNotificationsListener { @@ -24,6 +25,11 @@ class BrazeListener: PushNotificationsListener { func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) { guard let dictionary = userInfo as? [String: AnyHashable], shouldListenNotification(userinfo: userInfo) else { return } + + if let segmentService = Container.shared.resolve(SegmentAnalyticsService.self) { + segmentService.analytics?.receivedRemoteNotification(userInfo: userInfo) + } + let link = PushLink(dictionary: dictionary) deepLinkManager.processLinkFromNotification(link) } diff --git a/OpenEdX/Managers/PushNotificationsManager/Providers/BrazeProvider.swift b/OpenEdX/Managers/PushNotificationsManager/Providers/BrazeProvider.swift index 43ca0c780..3b93fec87 100644 --- a/OpenEdX/Managers/PushNotificationsManager/Providers/BrazeProvider.swift +++ b/OpenEdX/Managers/PushNotificationsManager/Providers/BrazeProvider.swift @@ -10,17 +10,20 @@ import SegmentBrazeUI import Swinject class BrazeProvider: PushNotificationsProvider { + func didRegisterWithDeviceToken(deviceToken: Data) { guard let segmentService = Container.shared.resolve(SegmentAnalyticsService.self) else { return } segmentService.analytics?.add( plugin: BrazeDestination( additionalConfiguration: { configuration in - configuration.logger.level = .debug + configuration.logger.level = .info }, additionalSetup: { braze in braze.notifications.register(deviceToken: deviceToken) } ) ) + + segmentService.analytics?.registeredForRemoteNotifications(deviceToken: deviceToken) } func didFailToRegisterForRemoteNotificationsWithError(error: Error) { diff --git a/OpenEdX/Managers/PushNotificationsManager/PushNotificationsManager.swift b/OpenEdX/Managers/PushNotificationsManager/PushNotificationsManager.swift index 1f6ab02b4..bd218ed02 100644 --- a/OpenEdX/Managers/PushNotificationsManager/PushNotificationsManager.swift +++ b/OpenEdX/Managers/PushNotificationsManager/PushNotificationsManager.swift @@ -29,6 +29,7 @@ class PushNotificationsManager: NSObject { private let deepLinkManager: DeepLinkManager private let storage: CoreStorage private let api: API + private var providers: [PushNotificationsProvider] = [] private var listeners: [PushNotificationsListener] = [] @@ -37,10 +38,16 @@ class PushNotificationsManager: NSObject { } // Init manager - public init(deepLinkManager: DeepLinkManager, storage: CoreStorage, api: API, config: ConfigProtocol) { + public init( + deepLinkManager: DeepLinkManager, + storage: CoreStorage, + api: API, + config: ConfigProtocol + ) { self.deepLinkManager = deepLinkManager self.storage = storage self.api = api + super.init() providers = providersFor(config: config) listeners = listenersFor(config: config) @@ -129,6 +136,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate { ) async -> UNNotificationPresentationOptions { if UIApplication.shared.applicationState == .active { didReceiveRemoteNotification(userInfo: notification.request.content.userInfo) + return [] } return [[.list, .banner, .sound]]