Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix double notification routing and segment callback #473

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion OpenEdX/DI/AppAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class AppAssembly: Assembly {
deepLinkManager: r.resolve(DeepLinkManager.self)!,
storage: r.resolve(CoreStorage.self)!,
api: r.resolve(API.self)!,
config: r.resolve(ConfigProtocol.self)!
config: r.resolve(ConfigProtocol.self)!,
segmentService: r.resolve(SegmentAnalyticsService.self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use an Analytics abstraction and not rely on the Segment dependency here?
We are going to migrate all third-party services to plugins, and this dependency could make it harder.

)
}.inObjectScope(.container)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import Foundation
class BrazeListener: PushNotificationsListener {

private let deepLinkManager: DeepLinkManager
private let segmentService: SegmentAnalyticsService?

init(deepLinkManager: DeepLinkManager) {
init(deepLinkManager: DeepLinkManager, segmentService: SegmentAnalyticsService?) {
self.segmentService = segmentService
self.deepLinkManager = deepLinkManager
}

Expand All @@ -24,6 +26,9 @@ class BrazeListener: PushNotificationsListener {
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) {
guard let dictionary = userInfo as? [String: AnyHashable],
shouldListenNotification(userinfo: userInfo) else { return }

segmentService?.analytics?.receivedRemoteNotification(userInfo: userInfo)

let link = PushLink(dictionary: dictionary)
deepLinkManager.processLinkFromNotification(link)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@

import Foundation
import SegmentBrazeUI
import Swinject

class BrazeProvider: PushNotificationsProvider {
let segmentService: SegmentAnalyticsService?

init(segmentService: SegmentAnalyticsService?) {
self.segmentService = segmentService
}

func didRegisterWithDeviceToken(deviceToken: Data) {
guard let segmentService = Container.shared.resolve(SegmentAnalyticsService.self) else { return }
segmentService.analytics?.add(
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,28 @@ class PushNotificationsManager: NSObject {
private let deepLinkManager: DeepLinkManager
private let storage: CoreStorage
private let api: API
private let segemntService: SegmentAnalyticsService?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo segemntService -> segmentService


private var providers: [PushNotificationsProvider] = []
private var listeners: [PushNotificationsListener] = []


public var hasProviders: Bool {
providers.count > 0
}

// Init manager
public init(deepLinkManager: DeepLinkManager, storage: CoreStorage, api: API, config: ConfigProtocol) {
public init(deepLinkManager: DeepLinkManager,
storage: CoreStorage,
api: API,
config: ConfigProtocol,
segmentService: SegmentAnalyticsService?
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public init(deepLinkManager: DeepLinkManager,
storage: CoreStorage,
api: API,
config: ConfigProtocol,
segmentService: SegmentAnalyticsService?
) {
public init(
deepLinkManager: DeepLinkManager,
storage: CoreStorage,
api: API,
config: ConfigProtocol,
segmentService: SegmentAnalyticsService?
) {

self.deepLinkManager = deepLinkManager
self.storage = storage
self.api = api
self.segemntService = segmentService

super.init()
providers = providersFor(config: config)
listeners = listenersFor(config: config)
Expand All @@ -49,7 +59,7 @@ class PushNotificationsManager: NSObject {
private func providersFor(config: ConfigProtocol) -> [PushNotificationsProvider] {
var pushProviders: [PushNotificationsProvider] = []
if config.braze.pushNotificationsEnabled {
pushProviders.append(BrazeProvider())
pushProviders.append(BrazeProvider(segmentService: segemntService))
}
if config.firebase.cloudMessagingEnabled {
pushProviders.append(FCMProvider(storage: storage, api: api))
Expand All @@ -60,7 +70,7 @@ class PushNotificationsManager: NSObject {
private func listenersFor(config: ConfigProtocol) -> [PushNotificationsListener] {
var pushListeners: [PushNotificationsListener] = []
if config.braze.pushNotificationsEnabled {
pushListeners.append(BrazeListener(deepLinkManager: deepLinkManager))
pushListeners.append(BrazeListener(deepLinkManager: deepLinkManager, segmentService: segemntService))
}
if config.firebase.cloudMessagingEnabled {
pushListeners.append(FCMListener(deepLinkManager: deepLinkManager))
Expand Down Expand Up @@ -129,6 +139,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
) async -> UNNotificationPresentationOptions {
if UIApplication.shared.applicationState == .active {
didReceiveRemoteNotification(userInfo: notification.request.content.userInfo)
return []
}

return [[.list, .banner, .sound]]
Expand Down
Loading