Launch from a mobile app
How to launch a Preference Center from a mobile app
When launching a Preference Center from a mobile app, the user is taken from your app to the Preference Center running in WebView. While the user is authenticated in your mobile app, this authentication gets lost when the Preference Center is loaded.
To launch a Preference Center from a mobile app and pass the user authentication information, you will need to:
  • Create a consent token for your Preference Center in your app backend
  • Launch your preference center in a WebView

Create a consent token and an authenticated URL in your app backend

A consent token is generated by sending an API request to the Didomi API that includes the user information (in particular, the organization user ID).
To create a consent token, send an HTTP POST request to the Didomi API:
1
POST https://api.didomi.io/consents/tokens?organization_id=<ID of your organization>
2
{
3
"organization_id": "<ID of your organization>",
4
"organization_user_id": "<User ID>",
5
"lifetime": 3600
6
}
Copied!
As the private API key must remain private, your app must first call you app backend in an authenticated context and your app backend can then call the Didomi API:
Once you have obtained a consent token, you can generate an authenticated URL to the Preference Center by adding the consent token to the Preference Center URL in a token query-string parameter.
We recommend that your backend returns the full authenticated URL with the token so that your app can simply launch that URL and does not hardcode any part of the URL.

Firebase

If your app is using Firebase, you can easily create an authenticated URL via a Cloud Function. We provide a Cloud Function to create a Preference Center URL that can be deployed to your Firebase account: https://github.com/didomi/firebase
The createPreferenceCenterURL Cloud Function will return the authenticated Preference Center URL with the user email from Firebase Authentication. It requires users to be authenticated via Firebase Authentication.
Android (Java)
Android (Kotlin)
iOS (Swift)
1
private Task<String> getPreferenceCenterURL() {
2
return mFunctions
3
.getHttpsCallable("createPreferenceCenterURL")
4
.call()
5
.continueWith(new Continuation<HttpsCallableResult, String>() {
6
@Override
7
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
8
// This continuation runs on either success or failure, but if the task
9
// has failed then getResult() will throw an Exception which will be
10
// propagated down.
11
String preferenceCenterURL = (String) task.getResult().getData();
12
return preferenceCenterURL;
13
}
14
});
15
}
Copied!
1
private fun getPreferenceCenterURL(): Task<String> {
2
return functions
3
.getHttpsCallable("createPreferenceCenterURL")
4
.call()
5
.continueWith { task ->
6
// This continuation runs on either success or failure, but if the task
7
// has failed then result will throw an Exception which will be
8
// propagated down.
9
val preferenceCenterURL = task.result?.data as String
10
preferenceCenterURL
11
}
12
}
Copied!
1
functions.httpsCallable("createPreferenceCenterURL").call() { (result, error) in
2
if let error = error as NSError? {
3
if error.domain == FunctionsErrorDomain {
4
let code = FunctionsErrorCode(rawValue: error.code)
5
let message = error.localizedDescription
6
let details = error.userInfo[FunctionsErrorDetailsKey]
7
}
8
// ...
9
}
10
if let text = (result?.data as? [String: Any])?["text"] as? String {
11
self.resultField.text = text
12
}
13
}
Copied!

Launch a Preference Center

Once you have obtained an authenticated URL to the Preference Center, simply open it in a WebView or in the device browser.
Android (WebView)
Android (Browser / Intent)
1
import android.view.View;
2
import android.webkit.WebView;
3
import android.webkit.WebViewClient;
4
5
/**
6
* Click handler to call when the user clicks on the menu item/link
7
* that should open the Preference Center
8
*
9
* We assume that you have a WebView created with ID preferenceCenterWebView
10
* and the correct Internet permissions as per https://developer.android.com/guide/webapps/webview.html
11
*/
12
public void preferenceCenterClickHandler(View view) {
13
String preferenceCenterURL = ...; // Get the Preference Center URL as described above
14
WebView preferenceCenterWebView = (WebView) findViewById(R.id.preferenceCenterWebView);
15
16
// Ensure that the user stays in the WebView when he navigates
17
preferenceCenterWebView.setWebViewClient(new WebViewClient());
18
19
// Enable JavaScript
20
preferenceCenterWebView.getSettings().setJavaScriptEnabled(true);
21
22
// Load the URL
23
preferenceCenterWebView.loadUrl(preferenceCenterURL);
24
25
// TODO: Actually show the WebView here
26
}
Copied!
1
import android.content.Intent;
2
import android.net.Uri;
3
import android.view.View;
4
5
/**
6
* Click handler to call when the user clicks on the menu item/link
7
* that should open the Preference Center
8
*/
9
public void privacyCenterClickHandler(View view) {
10
String preferenceCenterURL = ...; // Get the Preference Center URL as described above
11
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(preferenceCenterURL));
12
startActivity(launchBrowser);
13
}
Copied!