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:
POST https://api.didomi.io/consents/tokens?organization_id=<ID of your organization>{"organization_id": "<ID of your organization>","organization_user_id": "<User ID>","lifetime": 3600}
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.
privateTask<String>getPreferenceCenterURL() {return mFunctions.getHttpsCallable("createPreferenceCenterURL").call().continueWith(newContinuation<HttpsCallableResult,String>() { @OverridepublicStringthen(@NonNullTask<HttpsCallableResult> task) throwsException {// This continuation runs on either success or failure, but if the task// has failed then getResult() will throw an Exception which will be// propagated down.String preferenceCenterURL = (String) task.getResult().getData();return preferenceCenterURL; } });}
privatefungetPreferenceCenterURL(): Task<String> {return functions .getHttpsCallable("createPreferenceCenterURL") .call() .continueWith { task ->// This continuation runs on either success or failure, but if the task// has failed then result will throw an Exception which will be// propagated down.val preferenceCenterURL = task.result?.dataas String preferenceCenterURL }}
Once you have obtained an authenticated URL to the Preference Center, simply open it in a WebView or in the device browser.
importandroid.view.View;importandroid.webkit.WebView;importandroid.webkit.WebViewClient;/** * Click handler to call when the user clicks on the menu item/link * that should open the Preference Center * * We assume that you have a WebView created with ID preferenceCenterWebView * and the correct Internet permissions as per https://developer.android.com/guide/webapps/webview.html */publicvoidpreferenceCenterClickHandler(View view) {String preferenceCenterURL =...; // Get the Preference Center URL as described aboveWebView preferenceCenterWebView = (WebView) findViewById(R.id.preferenceCenterWebView);// Ensure that the user stays in the WebView when he navigatespreferenceCenterWebView.setWebViewClient(newWebViewClient());// Enable JavaScriptpreferenceCenterWebView.getSettings().setJavaScriptEnabled(true);// Load the URLpreferenceCenterWebView.loadUrl(preferenceCenterURL);// TODO: Actually show the WebView here}
importandroid.content.Intent;importandroid.net.Uri;importandroid.view.View;/** * Click handler to call when the user clicks on the menu item/link * that should open the Preference Center */publicvoidprivacyCenterClickHandler(View view) {String preferenceCenterURL =...; // Get the Preference Center URL as described aboveIntent launchBrowser =newIntent(Intent.ACTION_VIEW,Uri.parse(preferenceCenterURL));startActivity(launchBrowser);}