Skip to content

Commit ab43ae0

Browse files
committed
observe firebase's activated event to send notification for configuration changed
1 parent 17394b6 commit ab43ae0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Notification.Name+Extensions.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/02/04.
6+
//
7+
8+
import Foundation
9+
10+
// FirebaseRemoteConfig internal notification name, see also https://github.com/firebase/firebase-ios-sdk/blob/main/FirebaseRemoteConfig/Swift/PropertyWrapper/RemoteConfigValueObservable.swift
11+
extension Notification.Name {
12+
// Listens to FirebaseRemoteConfig SDK if new configs are activated.
13+
static let onRemoteConfigActivated = Notification.Name("FIRRemoteConfigActivateNotification")
14+
}

Sources/FirebaseRemoteConfigOpenFeatureProvider/FirebaseRemoteConfigOpenFeatureProvider.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import OpenFeature
55
import FirebaseRemoteConfig
6+
import FirebaseCore
67

78
public let firebaseRemoteConfigOpenFeatureProviderStaleTimeIntervalKey = "firebaseRemoteConfigOpenFeatureProviderStaleTimeIntervalKey"
89
public let firebaseRemoteConfigOpenFeatureProviderOldContextKey = "firebaseRemoteConfigOpenFeatureProviderOldContextKey"
@@ -41,6 +42,7 @@ public final class FirebaseRemoteConfigOpenFeatureProvider: FeatureProvider {
4142
public init(remoteConfig: RemoteConfigCompatible) {
4243
self.remoteConfig = remoteConfig
4344
updateStatus(for: remoteConfig)
45+
NotificationCenter.default.addObserver(self, selector: #selector(remoteConfigDidActivated), name: .onRemoteConfigActivated, object: nil)
4446
}
4547

4648
public func initialize(initialContext: EvaluationContext?) {
@@ -141,3 +143,22 @@ extension FirebaseRemoteConfigOpenFeatureProvider {
141143
OpenFeatureAPI.shared.emitEvent(event, provider: self, error: error, details: details)
142144
}
143145
}
146+
147+
extension FirebaseRemoteConfigOpenFeatureProvider {
148+
@objc func remoteConfigDidActivated(notification: Notification) {
149+
// Make sure this key is consistent with kFIRGoogleAppIDKey in FirebaseCore SDK
150+
// see also https://github.com/firebase/firebase-ios-sdk/blob/main/FirebaseRemoteConfig/Swift/PropertyWrapper/RemoteConfigValueObservable.swift
151+
let firebaseRemoteConfigAppNameKey = "FIRAppNameKey"
152+
153+
guard let appName = notification.userInfo?[firebaseRemoteConfigAppNameKey] as? String,
154+
FirebaseApp.app()?.name == appName else {
155+
return
156+
}
157+
158+
if status != .ready {
159+
status = .ready
160+
}
161+
162+
emit(event: .configurationChanged)
163+
}
164+
}

0 commit comments

Comments
 (0)