Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add EXTRA_REFERRER to CustomTabsIntent builder in Chrome custom tab for Android

I am using newly launched Chrome Custom tabs for android instead of using webviews. This is the link to their documentation

Here is the code that shows how to use it.

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

Question is that I want to add Intent.EXTRA_REFERRER to this. below is the para copied from their blog in their own words..

It's usually very important for websites to track where their traffic is coming from. Make sure you let them know you are sending them users by setting the referrer when launching your Custom Tab

intent.putExtra(Intent.EXTRA_REFERRER, 
             Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));

I failed to figure out any intent created to launch custom tabs.. Where to add this line?? If somebody has came across this, help please.

like image 418
Palak Darji Avatar asked Dec 19 '22 16:12

Palak Darji


1 Answers

you can put the extra on the Intent that is inside the CustomTabsIntent created by the builder like the following:

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
        Uri.parse("android-app://" + context.getPackageName()));
customTabsIntent.launchUrl(this, Uri.parse(url));

Explanation: Under the hood, a Custom tab is opened by using regular a Intent with a set of Extras that configure the UI customization. It's possible to see more on how it works at the Low Level API section of the docs. When CustomTabsIntent.Builder#build() is called, it creates a CustomTabsIntent, with a properly configured Intent inside it. This intent is still exposed by the API and that's what we use to add the EXTRA_REFERRER.

like image 200
andreban Avatar answered Apr 30 '23 16:04

andreban