-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
poc: Dynamic config #281
Open
MAKARD
wants to merge
1
commit into
braze-inc:master
Choose a base branch
from
MAKARD:poc/dynamic-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
poc: Dynamic config #281
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
BRAZE_API_KEY_ANDROID_ROI= | ||
BRAZE_API_KEY_ANDROID_UK= | ||
BRAZE_API_KEY_IOS_ROI= | ||
BRAZE_API_KEY_IOS_UK= | ||
BRAZE_ENDPOINT_ROI= | ||
BRAZE_ENDPOINT_UK= | ||
BRAZE_FIREBASE_CLOUD_MESSAGING_SENDER_ID_KEY_ANDROID= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
BrazeProject/android/app/src/main/java/com/brazeproject/braze/BrazeDynamicConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package com.brazeproject.braze | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import com.braze.Braze | ||
import com.braze.BrazeActivityLifecycleCallbackListener | ||
import com.braze.configuration.BrazeConfig | ||
import com.braze.configuration.BrazeConfigurationProvider | ||
import com.braze.support.BrazeLogger | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import kotlinx.coroutines.tasks.await | ||
import org.json.JSONObject | ||
|
||
data class ConfigData( | ||
private val _apiKey: String?, | ||
private val _endpoint: String?, | ||
private val _logLevel: Int?, | ||
private val _firebaseCloudMessagingSenderIdKey: String? | ||
) { | ||
var apiKey: String | ||
var endpoint: String | ||
var logLevel: Int | ||
var firebaseCloudMessagingSenderIdKey: String | ||
|
||
init { | ||
if ( | ||
_apiKey == null || | ||
_endpoint == null || | ||
_logLevel == null || | ||
_firebaseCloudMessagingSenderIdKey == null | ||
) { | ||
throw IllegalArgumentException( | ||
"Given config attributes are invalid" | ||
) | ||
} | ||
|
||
this.apiKey = _apiKey | ||
this.endpoint = _endpoint | ||
this.logLevel = _logLevel | ||
this.firebaseCloudMessagingSenderIdKey = _firebaseCloudMessagingSenderIdKey | ||
} | ||
|
||
companion object { | ||
fun fromJsonString(jsonString: String): ConfigData { | ||
return with(JSONObject(jsonString)) { | ||
ConfigData( | ||
getString("apiKey"), | ||
getString("endpoint"), | ||
getInt("logLevel"), | ||
getString("firebaseCloudMessagingSenderIdKey") | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun toJSONString(): String { | ||
return JSONObject().apply { | ||
put("apiKey", apiKey) | ||
put("endpoint", endpoint) | ||
put("logLevel", logLevel) | ||
put("firebaseCloudMessagingSenderIdKey", firebaseCloudMessagingSenderIdKey) | ||
}.toString() | ||
} | ||
} | ||
|
||
const val SAVED_CONFIG_KEY = "braze_saved_config" | ||
|
||
class BrazeDynamicConfiguration(private val application: Application) { | ||
private val sharedPref = application.getSharedPreferences("BrazeDynamicConfiguration", Context.MODE_PRIVATE) | ||
|
||
companion object { | ||
var sharedInstance: BrazeDynamicConfiguration? = null | ||
} | ||
|
||
init { | ||
sharedInstance = this; | ||
} | ||
|
||
private val activityLifecycleCallbackListener = BrazeActivityLifecycleCallbackListener() | ||
|
||
@Throws | ||
fun saveConfig(map: ReadableMap) { | ||
if (sharedPref == null) { | ||
throw IllegalAccessException( | ||
"Trying to save into nullish shared preferences" | ||
) | ||
} | ||
|
||
val config = with(map) { | ||
ConfigData( | ||
getString("apiKey"), | ||
getString("endpoint"), | ||
getInt("logLevel"), | ||
getString("firebaseCloudMessagingSenderIdKey") | ||
) | ||
} | ||
|
||
with(sharedPref.edit()) { | ||
putString(SAVED_CONFIG_KEY, config.toJSONString()) | ||
apply() | ||
} | ||
} | ||
|
||
@Throws | ||
fun getSavedConfig(): ConfigData? { | ||
if (sharedPref == null) { | ||
throw IllegalAccessException( | ||
"Trying to read from nullish shared preferences" | ||
) | ||
} | ||
|
||
val jsonString = sharedPref.getString(SAVED_CONFIG_KEY, null) | ||
?: return null | ||
|
||
return ConfigData.fromJsonString(jsonString) | ||
} | ||
|
||
@Throws | ||
suspend fun initializeWithSavedConfig() { | ||
val savedConfig = getSavedConfig() | ||
?: throw IllegalAccessException( | ||
"No saved config" | ||
) | ||
|
||
initialize(savedConfig) | ||
} | ||
|
||
@Throws | ||
private suspend fun initialize(config: ConfigData) { | ||
val configurationProvider = BrazeConfigurationProvider(application); | ||
|
||
val configuredApiKey = Braze.getConfiguredApiKey(configurationProvider); | ||
|
||
// To avoid creating an instance with the same config as the existing one | ||
if (config.apiKey == configuredApiKey) { | ||
return; | ||
} | ||
|
||
/* | ||
If the api key is configured, then calling "initialize" means changing configurations | ||
and therefore preparing for creating a new instance | ||
*/ | ||
if (configuredApiKey != null) { | ||
// Delete previous push token to avoid receiving push notifications originating from previous instance | ||
FirebaseMessaging.getInstance().deleteToken().await() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Workaround 1: |
||
|
||
Braze.apply { | ||
// Wipe all the data created by previous instance, to avoid unexpected behavior of the new instance | ||
getInstance(application).registeredPushToken = null | ||
wipeData(application) | ||
} | ||
|
||
// Unregister previous callback listener to avoid callbacks execution duplications | ||
application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbackListener) | ||
} | ||
|
||
BrazeLogger.logLevel = config.logLevel | ||
|
||
val brazeConfig = BrazeConfig.Builder().apply { | ||
setApiKey(config.apiKey) | ||
setCustomEndpoint(config.endpoint) | ||
setAutomaticGeofenceRequestsEnabled(false) | ||
setIsLocationCollectionEnabled(true) | ||
setTriggerActionMinimumTimeIntervalSeconds(1) | ||
setHandlePushDeepLinksAutomatically(true) | ||
}.build() | ||
|
||
application.registerActivityLifecycleCallbacks(activityLifecycleCallbackListener) | ||
|
||
Braze.apply { | ||
configure(application, brazeConfig) | ||
// Need to enable the new instance after wiping up the data | ||
enableSdk(application) | ||
|
||
/* | ||
Creating new push token: get token creates a new one if the previous one is deleted | ||
*/ | ||
val newToken = FirebaseMessaging.getInstance().token.await() | ||
|
||
getInstance(application).apply { | ||
registeredPushToken = newToken | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../java/com/brazeproject/brazeDynamicConfigurationBridge/BrazeDynamicConfigurationBridge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.brazeproject.brazeDynamicConfigurationBridge | ||
|
||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.brazeproject.braze.BrazeDynamicConfiguration | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
class BrazeDynamicConfigurationBridge internal constructor(context: ReactApplicationContext) : | ||
ReactContextBaseJavaModule(context) { | ||
override fun getName(): String { | ||
return "BrazeDynamicConfigurationBridge" | ||
} | ||
|
||
@ReactMethod | ||
fun saveConfig(config: ReadableMap, promise: Promise) { | ||
try { | ||
val brazeDynamicConfiguration = BrazeDynamicConfiguration.sharedInstance | ||
?: throw IllegalStateException("No shared instance for BrazeDynamicConfiguration") | ||
|
||
brazeDynamicConfiguration.saveConfig(config) | ||
|
||
promise.resolve(null) | ||
} catch (e: Exception) { | ||
promise.reject("Error", e) | ||
} | ||
} | ||
|
||
@ReactMethod | ||
fun initializeWithSavedConfig(promise: Promise) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
try { | ||
val brazeDynamicConfiguration = BrazeDynamicConfiguration.sharedInstance | ||
?: throw IllegalStateException("No shared instance for BrazeDynamicConfiguration") | ||
|
||
brazeDynamicConfiguration.initializeWithSavedConfig() | ||
|
||
promise.resolve(null) | ||
} catch (e: Exception) { | ||
promise.reject("Error", e) | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...java/com/brazeproject/brazeDynamicConfigurationBridge/BrazeDynamicConfigurationPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.brazeproject.brazeDynamicConfigurationBridge | ||
|
||
import android.view.View | ||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.uimanager.ReactShadowNode | ||
import com.facebook.react.uimanager.ViewManager | ||
|
||
class BrazeDynamicConfigurationPackage : ReactPackage { | ||
override fun createViewManagers( | ||
reactContext: ReactApplicationContext | ||
): MutableList<ViewManager<View, ReactShadowNode<*>>> = mutableListOf() | ||
|
||
override fun createNativeModules( | ||
reactContext: ReactApplicationContext | ||
): MutableList<NativeModule> = listOf( | ||
BrazeDynamicConfigurationBridge(reactContext), | ||
).toMutableList() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
module.exports = { | ||
presets: ['module:@react-native/babel-preset'], | ||
plugins: [ | ||
['module:react-native-dotenv', { | ||
moduleName: 'react-native-dotenv', | ||
path: '.env', | ||
blocklist: null, | ||
allowlist: null, | ||
safe: true, | ||
allowUndefined: true | ||
}], | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
declare module 'react-native-dotenv' { | ||
export const BRAZE_API_KEY_IOS_UK: string; | ||
export const BRAZE_API_KEY_ANDROID_UK: string; | ||
export const BRAZE_API_KEY_IOS_ROI: string; | ||
export const BRAZE_API_KEY_ANDROID_ROI: string; | ||
export const BRAZE_ENDPOINT_UK: string; | ||
export const BRAZE_ENDPOINT_ROI: string; | ||
export const BRAZE_FIREBASE_CLOUD_MESSAGING_SENDER_ID_KEY_ANDROID: string; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SDK creates the instance regardless of configuration validity - i.e when braze.xml is absent and programmatic configuration is not executed yet, the SDK throws warnings to the console that API_KEY is empty