17
votes

I have developed an android app locally on my device (app not yet on android play store). I have the following logic to get deep link in MainActivity.

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, null)
            .addApi(AppInvite.API)
            .build();

    // Check if this app was launched from a deep link. Setting autoLaunchDeepLink to true
    // would automatically launch the deep link if one is found.
    boolean autoLaunchDeepLink = false;
    AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
            .setResultCallback(
                    new ResultCallback<AppInviteInvitationResult>() {
                        @Override
                        public void onResult(@NonNull AppInviteInvitationResult result) {
                            if (result.getStatus().isSuccess()) {
                                // Extract deep link from Intent
                                Intent intent = result.getInvitationIntent();
                                String deepLink = AppInviteReferral.getDeepLink(intent);

                                Toast.makeText(getApplicationContext(), deepLink, Toast.LENGTH_LONG).show();
                                // Handle the deep link. For example, open the linked
                                // content, or apply promotional credit to the user's
                                // account.

                                // ...
                            } else {
                                Log.d(TAG, "getInvitation: no deep link found.");
                            }
                        }
                    });

I built some dynamic links using Firebase console and open in mobile browser. But it is not opening my app and reaching to line String deepLink = AppInviteReferral.getDeepLink(intent);

Instead it is opening the URL in mobile browser itself.

How to open the app and handle deep link in activity while using firebase dynamic link??

Edit:

I have intent filter in the manifest file.

<activity android:name="MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="example.com" android:scheme="http"/>
            <data android:host="example.com" android:scheme="https"/>
        </intent-filter>
    </activity>
5
Did you declare the intent filter in the manifest? Also, did you set up the console correctly with your SHA-1 fingerprint?NSimon
@NicolasSimon, yes i have intent filter in manifest. and setup console correctly. Updated detail in question.user3559471
Just a side note - be mindfull to put the android:host under <activity android:name="MainActivity"> (just like stated above) otherwise link will not open correctly. this was my issue.ilibilibom

5 Answers

11
votes

Action View and Action Main both are of different Intent category. So, you need to put them in different blocks like this:

 <activity android:name=".DynamicLinkActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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:host="example.com"
                android:scheme="http" />
            <data
                android:host="example.com"
                android:scheme="https" />
        </intent-filter>
    </activity>
8
votes

Alternatively, you could also provide the data in your intent-filter, like stated in the "regular" deep links doc (https://developer.android.com/training/app-indexing/deep-linking.html)

The intent filter would then look like the following :

        <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:host="https://XXYYZZ42.app.goo.gl/"  // <- Replace with your deep link from the console
                android:scheme="https"/>
        </intent-filter>

That said link can be obtained in your console, right here : enter image description here

EDIT : Added the picture to show where to find this link

3
votes

Update January 2019

Ensure that you have added the SHA256 certificate fingerprint for your app into your project in the Firebase console.

Add the below code in the AndroidManifest.xml under the activity which you want the dynamic link to lunch:

    <intent-filter android:autoVerify="true">
       <action android:name="android.intent.action.VIEW"/>
       <category android:name="android.intent.category.DEFAULT"/>
       <category android:name="android.intent.category.BROWSABLE"/>
       <data android:host="mydomainname.page.link" android:scheme="http"/>
       <data android:host="mydomainname.page.link" android:scheme="https"/>
   </intent-filter>
  • Remove android:autoVerify="true" if you have Android 6.0 (API level 23) and below or the app will crash.
2
votes

I had the same problem with deep links not working and the former answers wasn't helping. What did the trick was splitting the "data" tag like this:

Instead of:

<data android:host="example.com" android:scheme="http"/>
<data android:host="example.com" android:scheme="https"/>

Do this:

<data android:host="example.com"/>
<data android:scheme="http"/>
<data android:scheme="https"/>

Hope that will help anyone else :)

2
votes

As explained in another answer, your intent filter seems to have some problems. Also your url may have some problems. When I was playing with those, I had created faulty URL to FireBase web site without noticing it. It is possible to test you code by opening the whole url in your app. I wrote all urls to be tested to an email and sent to myself, open in the device and started clicking. After that you can create the url you want in FireBase. Below are few examples (typos and other errors possible):

If you clicked this url on your device:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp

and had this in you manifest:

 <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="https"
            android:host="mysite.fi"
            android:pathPattern=".*" />
    </intent-filter>

It should open https://mysite.fi/112972 in your app (com.mydomain.myapp) and if you opened the link on your PC, it would open https://mysite.fi/112972 in the browser.

If you opened this url in your device:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp&al=myscheme://mydeeplink/112972&afl=http://fallback.fi

and had this in you manifest:

 <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="myscheme"
            android:host="mydeeplink"
            android:pathPattern=".*" />
    </intent-filter>

It would open myscheme://mydeeplink/112972 in your app (com.mydomain.myapp). You would need to have code for handling it. If the app is not installed, it would open http://fallback.fi in your browser. On the PC it would still open https://mysite.fi/112972.

(edit 19.3.2018) It seems that Firebase does not fully support 'al=' anymore. The code works, but it is missing from the documentation and Firebase console generated urls.