Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-chekyrta committed Jun 20, 2024
1 parent 61ab13d commit f0866f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
//

import Foundation
import Swinject

class BrazeListener: PushNotificationsListener {

private lazy var deepLinkManager: DeepLinkManager? = {
Container.shared.resolve(DeepLinkManager.self)
}()

func shouldListenNotification(userinfo: [AnyHashable: Any]) -> Bool {
//A push notification sent from the braze has a key ab in it like ab = {c = "c_value";};
let data = userinfo["ab"] as? [String: Any]
return userinfo.count > 0 && data != nil
}

func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) {
guard let dictionary = userInfo as? [String: AnyHashable],
shouldListenNotification(userinfo: userInfo) else { return }
let link = PushLink(dictionary: dictionary)
deepLinkManager?.processLinkFromNotification(link)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@
//

import Foundation
import Swinject
import FirebaseMessaging

class FCMListener: PushNotificationsListener {

private lazy var deepLinkManager: DeepLinkManager? = {
Container.shared.resolve(DeepLinkManager.self)
}()

// check if userinfo contains data for this Listener
func shouldListenNotification(userinfo: [AnyHashable: Any]) -> Bool {
let data = userinfo["gcm.message_id"]
return userinfo.count > 0 && data != nil
}

func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) {
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)

guard let dictionary = userInfo as? [String: AnyHashable],
shouldListenNotification(userinfo: userInfo) else { return }
let link = PushLink(dictionary: dictionary)
deepLinkManager?.processLinkFromNotification(link)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ protocol PushNotificationsListener {
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any])
}

extension PushNotificationsListener {
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) {
guard let dictionary = userInfo as? [String: AnyHashable],
shouldListenNotification(userinfo: userInfo),
let deepLinkManager = Container.shared.resolve(DeepLinkManager.self)
else { return }
let link = PushLink(dictionary: dictionary)
deepLinkManager.processLinkFromNotification(link)
}
}

class PushNotificationsManager: NSObject {
private var providers: [PushNotificationsProvider] = []
private var listeners: [PushNotificationsListener] = []
Expand Down Expand Up @@ -132,13 +121,8 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification
) async -> UNNotificationPresentationOptions {
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(notification.request.content.userInfo)

// Show alert if application is active
if let pushManager = Container.shared.resolve(PushNotificationsManager.self),
UIApplication.shared.applicationState == .active {
pushManager.didReceiveRemoteNotification(userInfo: notification.request.content.userInfo)
if UIApplication.shared.applicationState == .active {
didReceiveRemoteNotification(userInfo: notification.request.content.userInfo)
}

return [[.list, .banner, .sound]]
Expand All @@ -150,10 +134,6 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
didReceive response: UNNotificationResponse
) async {
let userInfo = response.notification.request.content.userInfo

// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)

didReceiveRemoteNotification(userInfo: userInfo)
}
}

0 comments on commit f0866f5

Please sign in to comment.