Skip to content

Commit ad45cac

Browse files
take a shot at notification center for objective-c
1 parent 6ada4bb commit ad45cac

File tree

3 files changed

+159
-2
lines changed

3 files changed

+159
-2
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3434
self.userId = [NSString stringWithFormat:@"%d", arc4random()];
3535
self.attributes = @{ @"browser_type": @"safari" };
3636

37+
3738
// initialize SDK in one of these two ways:
3839
// (1) asynchronous SDK initialization (RECOMMENDED)
3940
// - fetch a JSON datafile from the server
@@ -59,6 +60,10 @@ -(void)initializeOptimizelySDKAsynchronous {
5960
self.optimizely = nil;
6061
}
6162

63+
[self.optimizely.notificationCenter addActivateNotificationListenerWithActivateListener:^(NSDictionary<NSString *,id> * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull variation, NSDictionary<NSString *,id> * _Nonnull event) {
64+
NSLog(@"got activate with experiment");
65+
NSLog(@"%@", experiment[@"key"]);
66+
}];
6267
[self startAppWithExperimentActivated];
6368
}];
6469
}

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,110 @@ open class OptimizelyManager: NSObject {
4949
return HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey)!
5050
}
5151
}
52-
52+
53+
@available(swift, obsoleted: 1.0)
54+
@objc(notificationCenter) public var objc_notificationCenter: Objc_OPTNotificationCenter {
55+
class ObjcCenter : Objc_OPTNotificationCenter {
56+
var notifications:OPTNotificationCenter
57+
58+
init(notificationCenter:OPTNotificationCenter) {
59+
notifications = notificationCenter
60+
}
61+
62+
func addActivateNotificationListener(activateListener: @escaping ([String : Any], String, [String : Any]?, [String : Any], Dictionary<String, Any>) -> Void) -> NSNumber? {
63+
64+
let num = notifications.addActivateNotificationListener { (experiment, userId, attributes, variation, event) in
65+
66+
let attrs = attributes?.mapValues({ (val) -> Any in
67+
if let val = val {
68+
return val
69+
}
70+
else {
71+
return NSNull()
72+
}
73+
})
74+
activateListener(experiment, userId, attrs, variation, event)
75+
}
76+
77+
if let num = num {
78+
return NSNumber(value: num)
79+
}
80+
81+
return nil
82+
}
83+
84+
func addTrackNotificationListener(trackListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>?, Dictionary<String, Any>) -> Void) -> NSNumber? {
85+
let num = notifications.addTrackNotificationListener { (eventKey, userId, attributes, eventTags, event) in
86+
87+
let attrs = attributes?.mapValues({ (val) -> Any in
88+
if let val = val {
89+
return val
90+
}
91+
else {
92+
return NSNull()
93+
}
94+
})
95+
96+
trackListener(eventKey, userId, attrs, eventTags, event)
97+
98+
}
99+
100+
if let num = num {
101+
return NSNumber(value: num)
102+
}
103+
104+
return nil
105+
}
106+
107+
func addDecisionNotificationListener(decisionListener: @escaping (String, String, [String : Any]?, Dictionary<String, Any>) -> Void) -> NSNumber? {
108+
109+
let num = notifications.addDecisionNotificationListener { (type, userId, attributes, decisionInfo) in
110+
let attrs = attributes?.mapValues({ (val) -> Any in
111+
if let val = val {
112+
return val
113+
}
114+
else {
115+
return NSNull()
116+
}
117+
})
118+
decisionListener(type, userId, attrs, decisionInfo)
119+
}
120+
if let num = num {
121+
return NSNumber(value: num)
122+
}
123+
124+
return nil
125+
}
126+
127+
func addDatafileChangeNotificationListener(datafileListener: @escaping (Data) -> Void) -> NSNumber? {
128+
let num = notifications.addDatafileChangeNotificationListener { (data) in
129+
datafileListener(data)
130+
}
131+
if let num = num {
132+
return NSNumber(value: num)
133+
}
134+
135+
return nil
136+
}
137+
138+
func removeNotificationListener(notificationId: Int) {
139+
notifications.removeNotificationListener(notificationId: notificationId)
140+
}
141+
142+
func clearNotificationListeners(type: NotificationType) {
143+
notifications.clearNotificationListeners(type: type)
144+
}
145+
146+
func clearAllNotificationListeners() {
147+
notifications.clearAllNotificationListeners()
148+
}
149+
150+
151+
}
152+
153+
return ObjcCenter(notificationCenter: self.notificationCenter)
154+
}
155+
53156
private let reInitLock = Dispatch.DispatchSemaphore(value: 1)
54157

55158
// MARK: - Public interfaces

OptimizelySDK/Protocols/OPTNotificationCenter.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import Foundation
1818

1919
/// Enum representing notification types.
20-
public enum NotificationType : Int {
20+
@objc public enum NotificationType : Int {
2121
case Activate = 1
2222
case Track
2323
case DatafileChange
@@ -109,3 +109,52 @@ func sendNotifications(type:Int, args:Array<Any?>)
109109

110110
}
111111

112+
@objc(OPTNotificationCenter) public protocol Objc_OPTNotificationCenter {
113+
/**
114+
Add an activate notification listener to the notification center.
115+
- Parameter activateListener: Notification to add.
116+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
117+
*/
118+
func addActivateNotificationListener(activateListener:@escaping (_ experiment:[String:Any], _ userId:String, _ attributes: [String:Any]?, _ variation:[String:Any], _ event:Dictionary<String, Any>) -> Void) -> NSNumber?
119+
120+
/**
121+
Add a track notification listener to the notification center.
122+
- Parameter trackListener: Notification to add.
123+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
124+
*/
125+
func addTrackNotificationListener(trackListener:@escaping (_ eventKey:String, _ userId:String, _ attributes: [String:Any]?, _ eventTags:Dictionary<String, Any>?, _ event:Dictionary<String, Any>) -> Void) -> NSNumber?
126+
127+
/**
128+
Add a decision notification listener to the notification center.
129+
- Parameter decisionListener: Notification to add.
130+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
131+
*/
132+
func addDecisionNotificationListener(decisionListener:@escaping (_ type:String, _ userId:String, _ attributes: [String:Any]?, _ decisionInfo:Dictionary<String, Any>) -> Void) -> NSNumber?
133+
134+
/**
135+
Add a datafile change notification listener
136+
- Parameter datafileChangeListener: Notification to add.
137+
- Returns: the notification id used to remove the notification. It is greater than 0 on success.
138+
*/
139+
func addDatafileChangeNotificationListener(datafileListener:@escaping (_ datafile:Data) -> Void) -> NSNumber?
140+
141+
/**
142+
Remove the notification listener based on the notificationId passed back from addNotification.
143+
- Parameter notificationId: the id passed back from add notification.
144+
- Returns: true if removed otherwise false (if the notification is already removed, it returns false).
145+
*/
146+
func removeNotificationListener(notificationId:Int)
147+
148+
/**
149+
Clear notification listeners by notification type.
150+
- Parameter type: type of OPTLYNotificationType to remove.
151+
*/
152+
func clearNotificationListeners(type:NotificationType)
153+
154+
/**
155+
* Clear out all the notification listeners.
156+
*/
157+
func clearAllNotificationListeners()
158+
159+
}
160+

0 commit comments

Comments
 (0)