Thursday, 29 December 2022

Android deep linking not loading from browser, even from simple tutorial

I'm having trouble getting deep linking to work in my android application, so I found the most bare-bones tutorial I could and it's still just opening the browser when I click on the link. All of this is pasted from my code not the tutorial, in case I pasted something wrong. Even with this

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.DeepLinkTest">
    <activity android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

        <!-- as we want to open main activity from our link so we are specifying
            only in main activity or we can specify that in different activity as well -->
        <!-- on below line we are adding intent filter to our MainActivity -->
        <intent-filter>
            <!-- below line is to set the action to our intent to view -->
            <action android:name="android.intent.action.VIEW" />

            <!-- on below line we are adding a default category to our intent -->
            <category android:name="android.intent.category.DEFAULT" />

            <!-- on below line we are adding a category to make our app browsable -->
            <category android:name="android.intent.category.BROWSABLE" />

            <!-- on below line we are specifying the host name and
                the scheme type from which we will be calling our app -->
            <data
                android:host="www.chaitanyamunje.com"
                android:scheme="https" />
        </intent-filter>

        <!-- below is the same filter as above just the scheme is changed to http -->
        <!-- so we can open our app with the url starting with https and http as well -->
        <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="www.chaitanyamunje.com"
                android:scheme="http" />
        </intent-filter>
    </activity>
</application>

MainActivity.kt

package me.paxana.deeplinktest

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {

  // creating a variable for our text view
  private lateinit var messageTV: TextView
  private var uri: Uri? = null

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // initializing our variable
    messageTV = findViewById(R.id.idTVMessage)
    // getting the data from our intent in our uri.
    uri = intent.data

    // checking if the uri is null or not.
    if (uri != null) {
      // if the uri is not null then we are getting
      // the path segments and storing it in list.
      val parameters = uri!!.pathSegments

      // after that we are extracting string
      // from that parameters.
      val param = parameters[parameters.size - 1]

      // on below line we are setting that
      // string to our text view which
      // we got as params.
      messageTV.text = param
    }
  }
}

The result of using the App Link Testing feature of android studio to load https://www.chaitanyamunje.com/hello/GeeksForGeeks

Browser showing "This site can't be reached. Check if there is a typo in chaitanyamunje.com. DNS_PROBE_FINISHED_NXDOMAIN."



from Android deep linking not loading from browser, even from simple tutorial

No comments:

Post a Comment