@@ -20,8 +20,14 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
2020 public var notificationId : Int = 1
2121 var notificationListeners = [ Int: ( Int, GenericListener) ] ( )
2222
23+ var observerLogEvent : NSObjectProtocol ?
24+
2325 required public init ( ) {
24-
26+ addInternalNotificationListners ( )
27+ }
28+
29+ deinit {
30+ removeInternalNotificationListners ( )
2531 }
2632
2733 internal func incrementNotificationId( ) -> Int {
@@ -116,6 +122,23 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
116122 return incrementNotificationId ( )
117123 }
118124
125+ public func addLogEventNotificationListener( logEventListener: @escaping LogEventListener ) -> Int ? {
126+ notificationListeners [ notificationId] = ( NotificationType . logEvent. rawValue, { ( args: Any... ) in
127+ guard let myArgs = args [ 0 ] as? [ Any ? ] else {
128+ return
129+ }
130+ if myArgs. count < 2 {
131+ return
132+ }
133+ if let url = myArgs [ 0 ] as? String ,
134+ let event = myArgs [ 1 ] as? [ String : Any ] {
135+ logEventListener ( url, event)
136+ }
137+ } )
138+
139+ return incrementNotificationId ( )
140+ }
141+
119142 public func removeNotificationListener( notificationId: Int ) {
120143 self . notificationListeners. removeValue ( forKey: notificationId)
121144 }
@@ -135,3 +158,31 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
135158 }
136159
137160}
161+
162+ // MARK: Notification Translation
163+
164+ extension DefaultNotificationCenter {
165+
166+ func addInternalNotificationListners( ) {
167+ observerLogEvent = NotificationCenter . default. addObserver ( forName: . willSendOptimizelyEvents, object: nil , queue: nil ) { ( notif) in
168+ guard let eventForDispatch = notif. object as? EventForDispatch else { return }
169+
170+ let url = eventForDispatch. url. absoluteString
171+ let eventData = eventForDispatch. body
172+
173+ if let event = try ? JSONSerialization . jsonObject ( with: eventData, options: [ ] ) as? [ String : Any ] {
174+ let args : [ Any ] = [ url, event]
175+ self . sendNotifications ( type: NotificationType . logEvent. rawValue, args: args)
176+ } else {
177+ print ( " LogEvent notification discarded due to invalid event " )
178+ }
179+ }
180+ }
181+
182+ func removeInternalNotificationListners( ) {
183+ if let observer = observerLogEvent {
184+ NotificationCenter . default. removeObserver ( observer, name: . willSendOptimizelyEvents, object: nil )
185+ }
186+ }
187+
188+ }
0 commit comments