Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting null value from intent in deep link

I have added a deep link to my app which maps an activity to a particular web page on my website (Link referred: https://developer.android.com/training/app-links/deep-linking ). While handling the deep link in my Java code, getAction() and getData() methods give me null value. I tried testing it here: https://firebase.google.com/docs/app-indexing/android/test (This gave me perfect result) But the same link opens in A web browser rather than in my app when clicked.

Android Manifest code :

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:exported="true">

    <tools:validation testUrl="https://www.mywebsite.com/xyz" />
    <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:scheme="https"
            android:host="www.mywebsite.com"
            android:pathPrefix="/xyz" />
    </intent-filter>
</activity>

Java Code :

    Intent intent = getIntent();
    String action = intent.getAction(); // getting null value here
    Uri data = intent.getData(); // getting null value here

I want that if the app is present, then it should be opened when the link is clicked or else the link will open the web page. Where and what am I missing?

like image 606
Anirudh Mishra Avatar asked Feb 07 '19 08:02

Anirudh Mishra


People also ask

Can intent be null?

therefore intent is always null in constructor. After setIntent(null): It's possible to change intent from outside of activity with setIntent() . In all other cases it can't.

What is deeplink redirect?

Deeplink Mobile allows mobile app users to generate a shareable link to a page in your app. The link is handled by a redirect page (from component Deeplink Web). On mobile, the redirect page opens your mobile app on a specific page using deeplinks or opens the App store / Play store to download your mobile app.

What is the difference between deep linking and deferred deep linking?

In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. Deferred deep linking allows users to deep link to content even if the app is not already installed.

What is deep linking attribution?

Deep linking is the extension or usage of attribution data. Let's go through a basic example of how it works to illuminate the tech. In the explanation previously, we talked about how a user may click a link or an ad: The attribution vendor then points the user to the appropriate app store to download the app.


2 Answers

You can run into this problem because using singleTask. In that case you should use onNewIntent to 'update' intent, because onCreate and onResume will not handle it.

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    //TODO: do something with new intent
}
like image 81
Tomas Ivan Avatar answered Sep 17 '22 08:09

Tomas Ivan


instead of getting intent data in onCreate, get in onResume

   @Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    if (intent != null && intent.getData() != null ){
        Toast.makeText(this, intent.getData().toString(), Toast.LENGTH_SHORT).show();
        webView.loadUrl(intent.getData().toString());
    }
}

add these lines of code which activity is attached for deep linking to open your website page

and mark it as a answer if it helped you to solve your problem

like image 43
Swapnil Musale Avatar answered Sep 19 '22 08:09

Swapnil Musale