forked from openedx/openedx-app-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added abstract layer for push notifications
- Loading branch information
Showing
14 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// BrazeConfig.swift | ||
// Core | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
private enum BrazeKeys: String { | ||
case enabled = "ENABLED" | ||
case pushNotificationsEnabled = "PUSH_NOTIFICATIONS_ENABLED" | ||
} | ||
|
||
public final class BrazeConfig: NSObject { | ||
public var enabled: Bool = false | ||
public var pushNotificationsEnabled: Bool = false | ||
|
||
init(dictionary: [String: AnyObject]) { | ||
super.init() | ||
enabled = dictionary[BrazeKeys.enabled.rawValue] as? Bool == true | ||
let pushNotificationsEnabled = dictionary[BrazeKeys.pushNotificationsEnabled.rawValue] as? Bool ?? false | ||
self.pushNotificationsEnabled = enabled && pushNotificationsEnabled | ||
} | ||
} | ||
|
||
private let brazeKey = "BRAZE" | ||
extension Config { | ||
public var braze: BrazeConfig { | ||
BrazeConfig(dictionary: self[brazeKey] as? [String: AnyObject] ?? [:]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
OpenEdX/Managers/PushNotificationsManager/Listeners/BrazeListener.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// BrazeListener.swift | ||
// OpenEdX | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
class BrazeListener: PushNotificationsListener { | ||
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) { | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
OpenEdX/Managers/PushNotificationsManager/Listeners/FCMListener.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// FCMListener.swift | ||
// OpenEdX | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
class FCMListener: PushNotificationsListener { | ||
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) { | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
OpenEdX/Managers/PushNotificationsManager/Providers/BrazeProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// BrazeProvider.swift | ||
// OpenEdX | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
class BrazeProvider: PushNotificationsProvider { | ||
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) { | ||
|
||
} | ||
func didFailToRegisterForRemoteNotificationsWithError(error: Error) { | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
OpenEdX/Managers/PushNotificationsManager/Providers/FCMProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// FCMProvider.swift | ||
// OpenEdX | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
class FCMProvider: PushNotificationsProvider { | ||
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) { | ||
|
||
} | ||
func didFailToRegisterForRemoteNotificationsWithError(error: Error) { | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
OpenEdX/Managers/PushNotificationsManager/PushNotificationsManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// PushNotificationManager.swift | ||
// OpenEdX | ||
// | ||
// Created by Anton Yarmolenka on 24/01/2024. | ||
// | ||
|
||
import Foundation | ||
import Core | ||
|
||
public protocol PushNotificationsProvider { | ||
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) | ||
func didFailToRegisterForRemoteNotificationsWithError(error: Error) | ||
} | ||
|
||
protocol PushNotificationsListener { | ||
func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) | ||
} | ||
|
||
class PushNotificationsManager { | ||
private var providers: [PushNotificationsProvider] = [] | ||
private var listeners: [PushNotificationsListener] = [] | ||
|
||
// Init manager | ||
public init(config: ConfigProtocol) { | ||
self.providers = self.providersFor(config: config) | ||
self.listeners = self.listenersFor(config: config) | ||
} | ||
|
||
private func providersFor(config: ConfigProtocol) -> [PushNotificationsProvider] { | ||
var rProviders: [PushNotificationsProvider] = [] | ||
if config.firebase.cloudMessagingEnabled { | ||
rProviders.append(FCMProvider()) | ||
} | ||
if config.braze.pushNotificationsEnabled { | ||
rProviders.append(BrazeProvider()) | ||
} | ||
return rProviders | ||
} | ||
|
||
private func listenersFor(config: ConfigProtocol) -> [PushNotificationsListener] { | ||
var rListeners: [PushNotificationsListener] = [] | ||
if config.firebase.cloudMessagingEnabled { | ||
rListeners.append(FCMListener()) | ||
} | ||
if config.braze.pushNotificationsEnabled { | ||
rListeners.append(BrazeListener()) | ||
} | ||
return rListeners | ||
} | ||
|
||
// Proccess functions from app delegate | ||
public func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) { | ||
for provider in providers { | ||
provider.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: deviceToken) | ||
} | ||
} | ||
public func didFailToRegisterForRemoteNotificationsWithError(error: Error) { | ||
for provider in providers { | ||
provider.didFailToRegisterForRemoteNotificationsWithError(error: error) | ||
} | ||
} | ||
public func didReceiveRemoteNotification(userInfo: [AnyHashable: Any]) { | ||
for listener in listeners { | ||
listener.didReceiveRemoteNotification(userInfo: userInfo) | ||
} | ||
} | ||
} |