Preference Center - optimove-tech/Optimove-SDK-Android GitHub Wiki

Enable Preference Center

To initialize Preference Center you need Preference Center SDK Credentials. You can find them in your Optimove dashboard Preference Center settings.

Note, for both delayed and standard initialization, Optimove credentials are also required for the Preference Center to work.

Standard Initialization

Enable Preference Center like so:

Optimove.initialize(this, new OptimoveConfig.Builder("optimoveCreds", "optimobileCreds")
            .enablePreferenceCenter("preferenceCenterCredentials")
            .build());

Delayed Initialization

Similar to here you can finish feature initialization later, for example, when your dynamically loaded credentials become available.

  1. Add Preference Center to the list of desired features in the config builder. Note that Optimove needs to be included in the set of features for the Preference Center to work:
OptimoveConfig.FeatureSet featureSet = new OptimoveConfig.FeatureSet().withOptimove().withPreferenceCenter();
Optimove.initialize(this, new OptimoveConfig.Builder(OptimoveConfig.Region.DEV, featureSet).build());

// time passes...

Optimove.setCredentials(
  optimoveCredentials: yourOptimoveCredentials,
  optimobileCredentials: yourOptimobileCredentials,
  preferenceCenterCredentials: yourPreferenceCenterCredentials
)

Get customer preferences

To fetch a list of customer preferences, use the getPreferencesAsync method. Here’s an example implementation:

OptimovePreferenceCenter.getInstance().getPreferencesAsync((OptimovePreferenceCenter.ResultType result, Preferences preferences) -> {
            switch (result) {
                case ERROR_USER_NOT_SET:
                case ERROR_CREDNTIALS_NOT_SET:
                case ERROR:
                    ...
                    break;
                case SUCCESS: {
                    ...
                    break;
                }
            }
        });

Preference Properties

Configured Channels (the channels configured for the brand group)

preferences.getConfiguredChannels() // List<Channel> e.g. MOBILE_PUSH, SMS

Customer Preferences

List<Topic> customerPreferences = preferences.getCustomerPreferences()
customerPreferences[0].getId() // topic id
customerPreferences[0].getName() // topic name
customerPreferences[0].getDescription() // topic description
customerPreferences[0].getSubscribedChannels() // List<Channel> channels the customer is subscribed to, e.g. MOBILE_PUSH, SMS

Set customer preferences

For updating customer preferences use setCustomerPreferencesAsync:

Example for updating preferences after getting the list of current preferences

OptimovePreferenceCenter
    .getInstance()
    .setCustomerPreferencesAsync((OptimovePreferenceCenter.ResultType setResult) -> {
       ...
    }, updates);

Complete Example: Getting and Setting Preferences

AtomicReference<Preferences> preferences = new AtomicReference<>();

OptimovePreferenceCenter.getInstance().getPreferencesAsync((OptimovePreferenceCenter.ResultType result, Preferences fetchPreferences) -> {
    switch (result) {
        case ERROR_USER_NOT_SET:
        case ERROR:
        case ERROR_CREDENTIALS_NOT_SET:
        // Handle errors
            break;
        case SUCCESS: {
            preferences.set(fetchPreferences);
            break;
        }
        default:
            // ...
        }
    });

if (preferences.get() == null) {
    return;
}

List<Channel> configuredChannels = preferences.get().getConfiguredChannels();
List<Topic> topics = preferences.get().getCustomerPreferences();

List<PreferenceUpdate> updates = new ArrayList<>();
for (int i = 0; i < topics.size(); i++) {
    // Note, you can only subscribe to channels that are configured
    updates.add(new PreferenceUpdate(topics.get(i).getId(), configuredChannels.subList(0, 1)));
}

OptimovePreferenceCenter.getInstance().setCustomerPreferencesAsync((OptimovePreferenceCenter.ResultType setResult) -> {
    // Handle result;
}, updates);
⚠️ **GitHub.com Fallback** ⚠️