---
title: Configure a notification listener using the Swift SDK
slug: feature-experimentation/docs/set-up-notification-listener-swift
docTags: 
createdAt: 2026-08-05T20:39:58.000Z
---

Notification listeners trigger a callback function that you define when certain actions are triggered in the SDK.

The most common use case is to send a stream of all feature flag decisions to an analytics provider or to an internal data warehouse to join it with other data that you have about your users.

## Notification listener types

For information about notification listener types and use cases, see [Notification listeners](docId:0d__cjuKECyJGqgTV0_5e).

The Swift SDK supports these notification types (`NotificationType`):

- `.decision` – Fires when the SDK makes a decision, for example through a Decide method.
- `.track` – Fires when the SDK tracks a conversion event.
- `.logEvent` – Fires just before the SDK sends an impression or conversion event batch to Optimizely. It does not confirm delivery.
- `.datafileChange` – Fires when a background fetch returns a datafile with a new revision. It does not fire on the initial `start`.
- `.activate` – Fires when the SDK sends an impression event for an experiment.

:::hint{type="info"}
**Note**
The follow code examples assume you have already [initialized the Swift SDK](docId\:fope5M9If2kPyLwtw-Yo1) and have created an `optimizelyClient` instance.
:::

## Add and remove all notification listeners

The following example code shows how to add a listener, remove a listener, remove all listeners of a specific type (such as all decision listeners), and remove all listeners.

```swift
// Add a notification listener
let notificationId = optimizely.notificationCenter?.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in    
    // Send data to analytics provider here
})  

// Remove a specific listener
optimizely.notificationCenter?.removeNotificationListener(notificationId: notificationId!)

// Remove all Notification Listeners
optimizely.notificationCenter?.clearAllNotificationListeners()  

// Remove all Notification Listeners of a certain type
optimizely.notificationCenter?.clearNotificationListeners(type: .decision)
```

```objective-c
// Add a notification listener
NSNumber *notificationId = [self.optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type, NSString *userId, NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *decisionInfo) {
    // Send data to analytics provider here
}];

// Remove a specific listener
[optimizely.notificationCenter removeNotificationListenerWithNotificationId:notificationId.integerValue];

// Remove all Notification Listeners
[optimizely.notificationCenter clearAllNotificationListeners];

// Remove all Notification Listeners of a certain type
[optimizely.notificationCenter clearNotificationListenersWithType:NotificationTypeDecision];
```

## Configure each type of notification listener

The following example code shows how to configure each type of notification listener.

```swift
// SET UP DECISION NOTIFICATION LISTENER
let onDecision : DecisionListener = {[weak self](decisionType, userId, attributes, decisionInfo) in
    // Add a DECISION Notification Listener for type FLAG
    if decisionType == "flag" {
       // Access information about feature, for example, key and enabled status
       print(decisionInfo["flagKey"])
       print(decisionInfo["enabled"])
       print(decisionInfo["decisionEventDispatched"])
     }
}
self.optimizelyClient?.notificationCenter?.addDecisionNotificationListener(decisionListener: onDecision)
// For "flag" decisions, decisionInfo contains these keys:
// flagKey, enabled, variables, variationKey, ruleKey, reasons,
// decisionEventDispatched, experimentId, variationId
        
// SET UP LOG EVENT NOTIFICATION LISTENER 
let onLogEvent : LogEventListener = {[weak self] (url, logEvent) in
    // process the logEvent object here (send to analytics provider, audit/inspect data)
}
self.optimizelyClient?.notificationCenter?.addLogEventNotificationListener(logEventListener: onLogEvent)
        
// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
// You will get notifications whenever the datafile is updated except for SDK initialization
let onDatafileUpdate : DatafileChangeListener = {[weak self] (datafile) in
}
self.optimizelyClient?.notificationCenter?.addDatafileChangeNotificationListener(datafileListener: onDatafileUpdate)
        
// SET UP TRACK LISTENER     
let onTrack : TrackListener = { [weak self](eventKey, userId, attributes, eventTags, event) in
    // process the event here (send to analytics provider, audit/inspect data)
}
self.optimizelyClient?.notificationCenter?.addTrackNotificationListener(trackListener: onTrack)
```

```objective-c
// SET UP ACTIVATE NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.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) {
}];

// SET UP DECISION NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addDecisionNotificationListenerWithDecisionListener: ^(NSString * _Nonnull decisionType, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
    // Add a DECISION Notification Listener for type FLAG
    if ([decisionType isEqualToString:@"flag"]) {
        // Access information about feature, for example, key and enabled status
        NSLog(@"%@", decisionInfo[@"flagKey"]);
        NSLog(@"%@", decisionInfo[@"enabled"]);
        NSLog(@"%@", decisionInfo[@"decisionEventDispatched"]);
    }
}];

// SET UP LOG EVENT NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addLogEventNotificationListenerWithLogEventListener:^(NSString * _Nonnull url, NSDictionary<NSString *,id> * _Nonnull logEvent) {
    // process the logEvent object here (send to analytics provider, audit/inspect data)
}];

// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addDatafileChangeNotificationListenerWithDatafileListener:^(NSData * _Nonnull datafile) {
}];
     
// SET UP TRACK LISTENER
    
(void)[self.optimizelyClient.notificationCenter addTrackNotificationListenerWithTrackListener:^(NSString * _Nonnull eventKey, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nullable eventTags, NSDictionary<NSString *,id> * _Nonnull event) {
    // process the event here (send to analytics provider, audit/inspect data)
}];
```
