Skip to content

Commit

Permalink
feat(push): Add multi push provider support for the wrapper on iOS side
Browse files Browse the repository at this point in the history
SUITEDEV-34531

Co-authored-by: megamegax <[email protected]>
Co-authored-by: matusekma <[email protected]>
  • Loading branch information
3 people committed Oct 19, 2023
1 parent 9b08e0c commit 5b07905
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 23 deletions.
9 changes: 9 additions & 0 deletions example/ios/NotificationExtension/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ import EmarsysNotificationService


class NotificationService: EMSNotificationService {

open override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
if request.content.userInfo.contains(where: {$0.key as! String == "customLogicKey"}) {
// do smth wih your other notificationservice logic
} else {
super.didReceive(request, withContentHandler: contentHandler)
}
}

}
2 changes: 1 addition & 1 deletion example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@
97C146EA1CF9000F007C117D /* Sources */,
9740EEB61CF901F6004384FC /* Run Script */,
97C146EB1CF9000F007C117D /* Frameworks */,
569CECB52642C4CF00D743F7 /* Embed App Extensions */,
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
569CECB52642C4CF00D743F7 /* Embed App Extensions */,
7A9D4E757E4196BDF07714C8 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
Expand Down
27 changes: 26 additions & 1 deletion example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@ import UIKit
import Flutter
import emarsys_sdk

class CustomDelegate: NSObject, UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("userNotificationCenter: didReceive: withCompletionHandler:")
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("userNotificationCenter: willPresent: withCompletionHandler:")
}

func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
print("userNotificationCenter: openSettingsFor:")
}

}

@UIApplicationMain
@objc class AppDelegate: EmarsysAppDelegate {

override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
let result = super.application(application, didFinishLaunchingWithOptions: launchOptions)

return result
}

override func notificationCenterDelegateDataSource() -> [UNUserNotificationCenterDelegate] {
let customDelegate = CustomDelegate()
let customDelegate2 = CustomDelegate()
return [customDelegate, customDelegate2]
}

}
6 changes: 5 additions & 1 deletion ios/Classes/EmarsysAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import EmarsysSDK
UNUserNotificationCenter.current().requestAuthorization(options: authorizationOptions) { granted, error in
}
UNUserNotificationCenter.current().delegate = UserNotificationCenterDelegateCacher.instance

notificationCenterDelegateDataSource().forEach { UserNotificationCenterDelegateCacher.instance.addDelegate(notificationCenterDelegate: $0) }
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

open override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
EmarsysPushTokenHolder.pushToken = deviceToken
}

open func notificationCenterDelegateDataSource() -> [UNUserNotificationCenterDelegate] {
return []
}
}
2 changes: 1 addition & 1 deletion ios/Classes/Wrapper/Commands/Setup/SetupCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class SetupCommand: EmarsysCommandProtocol {
}
}
}
UNUserNotificationCenter.current().delegate = Emarsys.push

UserNotificationCenterDelegateCacher.instance.emptyCache(with: Emarsys.push)

Emarsys.push.silentMessageEventHandler = self.silentPushEventHandler
Expand Down
104 changes: 85 additions & 19 deletions ios/Classes/Wrapper/Push/UserNotificationCenterDelegateCacher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,117 @@ public class UserNotificationCenterDelegateCacher: NSObject, UNUserNotificationC
private var didReceiveCache = [[String: Any]]()
private var willPresentCache = [[String: Any]]()
private var openSettingsCache = [[String: Any]]()
private var customDelegates = [any UNUserNotificationCenterDelegate]()
private var emarsysNotificationCenterDelegate: UNUserNotificationCenterDelegate?
private var caching = true

public static let instance = UserNotificationCenterDelegateCacher()

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
didReceiveCache.append([
"center": center,
"response": response,
"completionHandler": completionHandler])
public func userNotificationCenter(
_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
if caching {
didReceiveCache.append([
"center": center,
"response": response,
"completionHandler": completionHandler,
])
}
if isEmarsysNotification(notification: response.notification) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(
center, didReceive: response, withCompletionHandler: completionHandler)
} else {
customDelegates.forEach { delegate in
delegate.userNotificationCenter?(
center, didReceive: response, withCompletionHandler: completionHandler)
}
}
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
willPresentCache.append([
"center": center,
"notification": notification,
"completionHandler": completionHandler])
public func userNotificationCenter(
_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
if caching {
willPresentCache.append([
"center": center,
"notification": notification,
"completionHandler": completionHandler,
])
}
if isEmarsysNotification(notification: notification) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(
center, willPresent: notification, withCompletionHandler: completionHandler)
} else {
customDelegates.forEach { delegate in
delegate.userNotificationCenter?(
center, willPresent: notification, withCompletionHandler: completionHandler)
}
}
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
var dict = [String: Any]()
dict["center"] = center
if let noti = notification {
dict["notification"] = noti
public func userNotificationCenter(
_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?
) {
if caching {
var dict = [String: Any]()
dict["center"] = center
if let noti = notification {
dict["notification"] = noti
}
openSettingsCache.append(dict)
}
if isEmarsysNotification(notification: notification) {
if #available(iOS 12.0, *) {
emarsysNotificationCenterDelegate?.userNotificationCenter?(center, openSettingsFor: notification)
}
} else {
customDelegates.forEach { delegate in
if #available(iOS 12.0, *) {
delegate.userNotificationCenter?(center, openSettingsFor: notification)
}
}
}
openSettingsCache.append(dict)
}

public func addDelegate(notificationCenterDelegate: UNUserNotificationCenterDelegate) {
self.customDelegates.append(notificationCenterDelegate)
}

func emptyCache(with notificationCenterDelegate: UNUserNotificationCenterDelegate) {
self.emarsysNotificationCenterDelegate = notificationCenterDelegate
willPresentCache.forEach { cachedDict in
notificationCenterDelegate.userNotificationCenter?(cachedDict["center"] as! UNUserNotificationCenter, willPresent: cachedDict["notification"] as! UNNotification, withCompletionHandler: cachedDict["completionHandler"] as! (UNNotificationPresentationOptions) -> Void)
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter,
willPresent: cachedDict["notification"] as! UNNotification,
withCompletionHandler: cachedDict["completionHandler"]
as! (UNNotificationPresentationOptions) -> Void)
}
didReceiveCache.forEach { cachedDict in
notificationCenterDelegate.userNotificationCenter?(cachedDict["center"] as! UNUserNotificationCenter, didReceive: cachedDict["response"] as! UNNotificationResponse, withCompletionHandler: cachedDict["completionHandler"] as! () -> Void)
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter,
didReceive: cachedDict["response"] as! UNNotificationResponse,
withCompletionHandler: cachedDict["completionHandler"] as! () -> Void)
}
if #available(iOS 12.0, *) {
openSettingsCache.forEach { cachedDict in
var notification: UNNotification? = nil
if let noti = cachedDict["notification"] as? UNNotification {
notification = noti
}
notificationCenterDelegate.userNotificationCenter?(cachedDict["center"] as! UNUserNotificationCenter, openSettingsFor: notification)
notificationCenterDelegate.userNotificationCenter?(
cachedDict["center"] as! UNUserNotificationCenter, openSettingsFor: notification)
}
openSettingsCache.removeAll()
}
willPresentCache.removeAll()
didReceiveCache.removeAll()
self.caching = false
}

private func isEmarsysNotification(notification: UNNotification?) -> Bool {
return ((notification?.request.content.userInfo.contains(where: { $0.key as! String == "ems"})) != nil)
}

}

0 comments on commit 5b07905

Please sign in to comment.