Abhishek Singh
Abhishek Singh

Reputation: 33

How to find from where the android app deep link is click like it does in open referrer in web?

I need to find what was the source of my app open like(fb,twitter,g+) is there a way to find it. I know there is a open referrer in header of url but how can i fetch this information in android app

Upvotes: 1

Views: 4620

Answers (4)

Fadli
Fadli

Reputation: 976

Google provide online material for this: https://codelabs.developers.google.com/codelabs/deeplink-referrer

Summary

You can use Activity#getReferrer() to get the referrer of the deep link. It's available since API 22.

For API Level 21 and below, you can use the following snippet from above online material:

    private static final String REFERRER_NAME = "android.intent.extra.REFERRER_NAME";


 /** Returns the referrer on devices running SDK versions lower than 22. */
    private Uri getReferrerCompatible(Activity activity) {
        Intent intent = activity.getIntent();
        Uri referrerUri = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
        if (referrerUri != null) {
            return referrerUri;
        }
        String referrer = intent.getStringExtra(REFERRER_NAME);
        if (referrer != null) {
            // Try parsing the referrer URL; if it's invalid, return null
            try {
                return Uri.parse(referrer);
            } catch (ParseException e) {
                return null;
            }
        }
        return null;
    }

Upvotes: 1

Ahmed na
Ahmed na

Reputation: 1154

depending on @Ahmad Fadli answer (works but some application might not be detected) I had a similar use case, so someone might need it , you could use both of them too :

fun getReferrerCompatible(activity: Activity?): String? {
    if (activity == null)
        return null
    try {
        val intent = activity.intent
        val bundle = intent.extras
        if (bundle != null) {
            val keySet = bundle.keySet().toTypedArray()
            for (k in keySet) {
                if (k.contains("application_id")) {
                    return bundle.getString(k)
                }

            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            return activity.referrer?.toString()
        }
    } catch (e: Exception) {
        Crashlytics.logException(e)
    }
    return null
}

the default referrer actually does not give you chrome application for some deep links ,for some reason it would return "google.com"

Upvotes: 1

dindinii
dindinii

Reputation: 655

Try this

On your android manifest file register your acitivity which have to open on link click

 <activity
            android:name=".YOURACTIVITY"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http"
                    android:host="somedomainname.com"
                    android:pathPrefix="/index"/>

            </intent-filter>

        </activity>

On your html

<a href="android-app:/your android app packagename/http://somedomainname.com/index">Click me</a>

Upvotes: 0

gipsy
gipsy

Reputation: 3859

Take a look at Firebase Dynamic Links https://firebase.google.com/docs/dynamic-links/

Upvotes: 0

Related Questions