Firebase Dynamic Link open specific Activity
Asked Answered
L

3

6

I implemented Firebase Dynamic links (which are great!) on my iOS app and am now doing the same job on Android. I managed to launch my Android app by clicking the dynamic URL, but I can't open it on another activity than my launcher activity.

Here is my manifest.xml file :

    <activity android:name=".Activity.SplashActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Activity.RouteListActivity"
        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:host="mywebsite.com" android:scheme="http"/>
            <data android:host="mywebsite.com" android:scheme="https"/>
            <data
                android:host="myapp.app.goo.gl/"
                android:scheme="https" />
        </intent-filter>
    </activity>

When I click the URL, browser opens and redirects to my app but it opens on SplashActivity and not on RouteListActivity as expected.

Do I miss something?

Thank you

Larkin answered 13/12, 2017 at 13:24 Comment(8)
Remove the last / from android:host attributeMechanician
@SimonMarquis Okay I've deleted it, but no change. My app is still being opened by my launcher activity and not RouteListActivityLarkin
You shouldn't need the myapp.app.goo.gl entry - can you try removing that? (and check for any other trailing data on the mywebsite entries.Expediency
@IanBarber Okay I removed it but with no success. What do you mean with trailing data? Thank youLarkin
Can you try just going to my website.com directly (tap a link with it elsewhere) rather than via an FDL and check that works?Expediency
@GrayFox, have you ever found a solution to this issue? I'm dealing with the same behavior and I feel it has to do with the launcher property, but I'm not sure (something similar happened while testing the TV leanback launcher). Running the activity directly from the emulator works, though. If you have it figured out a way, please, let me know because I'd appreciate that.Kroll
@Kroll : No, I had to get round this issue by creating an intent calling the wanted Activity. This is done into my launcher activity through the FirebaseDynamicLinks OnSuccessListener interface.Larkin
@GrayFox, good to know! Thanks for the enlightenment about this issue. I'm going to give it a try soon.Kroll
K
3

Well, I guess I found a simple workaround that's able to overcome this problem.

I reminded of an app I developed in which the dynamic links work well in previous versions and I just tested them again to confirm (here is a sample dynamic link to demonstrate it really works); so I went to its manifest to know what I did back then.

Basically, it's important to add an android:autoVerify=true to the intent filter like this:

<intent-filter android:autoVerify="true">

It'll make a request to verify app links as you can read here in order to understand it better.

As this solution only works from API 23 on, I suggest to add tools:targetApi="23" too as "this tells the tools that you believe this element (and any children) will be used only on the specified API level or higher" and to avoid an unwanted Lint highlight then.

So, the best way to deal with this is by adding the following code:

<intent-filter
   android:autoVerify="true"
   tools:targetApi="23">

It's important to mention that "set as default" options from the user end might overlook this option depending on the way users use the link because it may appear a popup offering choices about how to handle the link and it can be somehow misleading (the two options look the same considering my app and device at least).

In the sample manifest file present in this question, it would look like this:

<activity android:name=".Activity.SplashActivity"
    android:theme="@style/SplashTheme"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".Activity.RouteListActivity"
    android:screenOrientation="portrait">
        <intent-filter
            android:autoVerify="true"
            tools:targetApi="23">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="mywebsite.com" android:scheme="http"/>
        <data android:host="mywebsite.com" android:scheme="https"/>
        <data
            android:host="myapp.app.goo.gl/"
            android:scheme="https" />
    </intent-filter>
</activity>

And don't forget to add xmlns:tools="http://schemas.android.com/tools" in the beginning of the manifest.

Kroll answered 27/2, 2018 at 5:48 Comment(6)
Well done Jorge, that works! However I guess that because we target API >=23, this won't work for previous API, is it?Larkin
Oops my bad, I didn't overwrite all my previous code. Unfortunately this code does not the trick in my app, while using API 25Larkin
Yes, I guess it's a feature put available after API 23 as Lint says. It's really weird as it works easily while testing 2/3 apps, but one still gets this wrong. I'm not sure, but I think it has to do with something about the homepage, but I still have to test. I'm using an alternative page to run the main activity and adding another activity to load the homepage and I think the homepage might be confused with the app itself. I'll do more tests as soon as I have time and I'll try to edit the answer if I find a solution. In one of my apps, I had to clear the "set as default" before it'd work.Kroll
I've done a quick test with the same app which uses the dynamic link I mentioned in the answer and a dynamic link to the homepage ignores the specific intent filter and just runs the default app main activity Here you have the two links to see: e5kmd.app.goo.gl/zvpW and e5kmd.app.goo.gl/Zy4N; the first one is for a page and works well and offers the option, but the second one which is directed to the clear homepage just runs the app as usual. If that's what's happening in your case too, I'd suggest to create a page similar to your homepage, but with a slug and avoid copying.Kroll
@Kroll I have followed the exact steps, but it's not working for me. I am still facing the issue. The app is always launching on the splash page rather than the intended dynamic link page on clicking the dynamic link. Did you make any improvements on your code?Eellike
Hi, @SethuramanSrinivasan. Sorry, unfortunately I dropped all Firebase features long time ago so I don't know whether it still works or not and it's hard to help you with this issue.Kroll
P
0

Following are the steps I followed to handle deep links in my application.

First

Add this to the activity (in manifest file) which will handle the Deep links for your app. For me it is my home screen activity

<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="yourDomain"
                android:scheme="http" />
            <data
                android:host="yourDomain"
                android:scheme="https" />
</intent-filter>

Next

Go to the activity to which you added the above code. In the onCreate method of that activity you'll handle the received data.

  • Create a dynamic link instance in the activity like this -

The code is in Kotlin

    FirebaseDynamicLinks.getInstance().getDynamicLink(intent).addOnSuccessListener{pendingDynamicLinkData ->

                    var deepLink: Uri? = null
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.link
                    }

                    val path = deepLink?.path
                    val query = deepLink?.encodedQuery

                    //You can log the path here
                    //Generally the path consists of the link extracted 
                    //from the deep link

                    val parts = path?.split("/")

                    if (parts?.get(2) == "something") {
                        val intent = Intent(this, YouActivity::class.java)
                        intent.putExtra("extra1", parts[3])
                                .putExtra("extra2", parts[1])
                        startActivity(intent)
                    } 
                }.addOnFailureListener { e -> //You can log the error here }

What have I done here?

First I created the instance of FirebaseDynamicLinks. Next, I used the fetched dynamic link data to extract the link (deepLink?.path) which is the link that opens a specific page on my website (say an article). Then, I split the link into pieces and used them as the information (the extras in the intent) to send to my activity and load the required data.

You can do this for all kinds of links. Receive the data on a receiving activity and redirect from there to any activity depending upon the data received.

I hope this helped to resolve your issue. Let me know if there is any confusion.

Thanks

Phyliciaphylis answered 31/8, 2018 at 6:6 Comment(0)
I
0

To avoid being redirected to your launch activity, you should add an intent-filter with the same host as configured in your dynamic link.

My dynamic link in Firebase

For the image above, the intent-filter I configured is below. Note, this Activity is not my launcher. A point of attention is to add "www" and your domain name space (com.br) in the host field at xml code.

Intent filter configured

Insure answered 5/10, 2022 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.