2

What I'd like to achieve

When a user clicks on any link from my website, I'd like it to direct the user specific link in my app (If it's installed). If it's not installed then direct the user to link in the browser.

Example:

User gets an email with a link in it called: www.myapp.com/someplaceinmyapp It goes to that page in my app if It's installed. If not, then go to that page in the browser.

Keep in mind that it will be for all pages in my app. So this applies for any link from my webpage.

My questions

Is there a specific name for this? I know about deep linking and app links, but I'm not sure which one is for me. 

Is this even possible? I'm not even sure if It's possible at the moment. 

Will the code for this be in the webage or the app? e.g: Javascript or Kotlin or something in the manifest file.

How do I do this? This is probably the thing I need the most.

Notes

I have got something in my webpage which shows a popup (only in browser) that says "Use the app". If not installed, then goes to the play store. If installed, then goes to the homepage of my website in my app. I found how to do it from here using the answer with the most votes (not the accepted one) so in my manifest I have:

        <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="www.myapp.com"
                android:pathPrefix="/install.html" />
        </intent-filter>

If there's anything you'd like me to add to this question, just let me know :)

Amy
  • 1,114
  • 13
  • 35

3 Answers3

2

I found that the way to do this, was using the App Links assistant, which you can find in android studio under Tools -> App Links assistant. This is what you should see on the side once you open it:

Step 1

Click "Open URL Mapping Editor", then add a URL mapping (Click the "+"). In the host, put your website link e.g: https://www.example.com. For Path, in the select box, select just "path" and put nothing in the type box.

Step 2

The Select Activity button does not work for Kotlin, so here's the code that you manually need to put in.

    override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    handleIntent(intent)
}

private fun handleIntent(intent: Intent) {
    val wbWebView = findViewById<View>(R.id.your_webview) as WebView
    val appLinkAction = intent.action
    val appLinkData = intent.data
    if (Intent.ACTION_VIEW == appLinkAction && appLinkData != null) {
            wbWebView.loadUrl(appLinkData.toString())
    } else {
        // default link that it goes to when opening app
        wbWebView.loadUrl("https://www.example.com")
    }
}

and in your onCreate add handleIntent(intent)

Step 3

Click the "Open Digital Asset Links File Generator". Most of the information will be already filled, so you can go on to click the "Generate Digital Asset Links File". Make a folder called ".well-known" in your website and then put a file in there called "assetlinks.json", paste the preview into there. So when you go to https://www.yourwebsite.com/.well-known/assetlinks.json you should be able to see the Digital asset links file. You can also see other websites Digital asset links file. Like for google, https://www.google.com/.well-known/assetlinks.json.

Then proceed to click the "Link and Verify" button, If all goes well you should see this appear beneath the button:

Step 4

Time to test! Go ahead and click on "Test app links" and put in a URL from your website. If all goes well, you shouldn't see a disambiguation dialogue.

I also like to do further testing by sending myself an email with the link in it.

Notes

You can only use the App Links Assistant if your website is using https instead of http.

I added android:launchMode="singleTask" into my activity tag in the manifest, this means that If you click on a link from your website in an email then it will open in a new window.

They have a doc about this here which includes a video (Keep in mind that they are using java)

Amy
  • 1,114
  • 13
  • 35
1

For entering specifc Activity or Fragment of your app from an url, using Deep Links from Navigation Component is recommended.

1. Add an Url Deep Link in Navigation resource file (from Attributes panel):

<fragment
    android:id="@+id/studentEditorFragment"
    android:name="com.example.roomtest.studentEditor.StudentEditorFragment"
    android:label="StudentEditorFragment"
    tools:layout="@layout/fragment_student_editor">

    <argument
        android:name="studentId"
        app:argType="long" />

    <deepLink
        android:id="@+id/deepLink"
        app:uri="www.example.com/roomTest/{studentId}" />       //here, company domain name followed by application path or argument

</fragment>

2. Add Navigation attribute in AndroidManifest file:

<activity android:name=".MainActivity">                 //【inside the "<activity>" tag】
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <nav-graph android:value="@navigation/navigation" />        //here
</activity>

3. Use the link in web browser or text message: "www.example.com/roomTest/-1"

  • For passing multiple arguments, just connect them by "&" like: "www.example.com/roomText/-1&true" (reformat "&" in xml as prompted).

Sam Chen
  • 7,597
  • 2
  • 40
  • 73
  • This is not what I'm looking for. As I this is for a specific link. I need something for all links that are from my website. – Amy Dec 26 '20 at 04:10
0

Use Android App links in your app. If implemented properly, user will be moved directly to the specified destination in the app.

More Information about this in these docs:

Verify Android App Links

Create an implicit deep link

Amy
  • 1,114
  • 13
  • 35
Alpha 1
  • 4,118
  • 2
  • 17
  • 23