Nativescript: Deep Linking

Created on 6 Mar 2017  路  10Comments  路  Source: NativeScript/NativeScript

Hello, I've been trying to figure out how to make deep linking work for the android platform. I have yet to fine the solution. I converted my app from the Iconic framework and I was able to get deep linking working with it. I was hoping you would be able to give me an idea on how to get deep linking to work.

  • CLI: 2.5.2
  • Cross-platform modules: 2.5.0
  • Runtime(s): android 2.5.0
android

Most helpful comment

I made a simple example repo here for deep-linking on iOS and Android with Nativescript & Angular:
https://github.com/ddfreiling/tns-ng-deeplink

All 10 comments

@mrdeleon @tsonevn In order to get deep linking to work on android,

Modify your AndroidManifest.xml:

  • Add android:launchMode="singleTask" to your app's activity section. This tells the Android operating system to launch the app with a new instance of the activity, or use an existing one. Without this your app will launch multiple instances of itself which is no good.
  • Add the following to the same activity section.
<intent-filter>
    <data android:scheme="YOUR DEEP LINK URL HERE" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

So your final application section might look something like this:

<application android:name="com.tns.NativeScriptApplication" android:allowBackup="true" android:icon="@drawable/icon"
    android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="com.tns.NativeScriptActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@style/LaunchScreenTheme" android:launchMode="singleTask" android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden|adjustResize">
        <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />
        <intent-filter>
            <data android:scheme="wegossip" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.tns.ErrorReportActivity" />
</application>

@roblav96 Thanks for the help.
I noticed that if the app is already opened the intents data is null, but if I closed it then open it with the custom scheme the data is there. Is there another way I should be getting the intents data if the app is already opened?

application.android.on(application.AndroidApplication.activityResumedEvent, (args: application.AndroidActivityEventData) => { 
      if(new String(args.activity.getIntent().getAction()).valueOf() ==  new String(android.content.Intent.ACTION_VIEW).valueOf()){
          console.log(args.activity.getIntent().getData()); 
      }
});

@mrdeleon did you every figure this out that is the issue i am running into

@mrdeleon @scavezze @NickIliev Sorry guys, I thought I replied to this :X

You'll want to use the onNewIntent method in your android.app.Activity class:

@JavaProxy('com.tns.NativeScriptActivity')
class Activity extends android.app.Activity {
    private _callbacks: AndroidActivityCallbacks
    protected onCreate(savedInstanceState: android.os.Bundle): void {
        if (!this._callbacks) {
            setActivityCallbacks(this)
        }
        this._callbacks.onCreate(this, savedInstanceState, super.onCreate)
    }
    protected onNewIntent(intent: android.content.Intent): void {
        super.onNewIntent(intent)
        if (intent.getDataString) {
            let data = intent.getDataString()
            if (data) {
                console.log('data', data)
            }
        }
    }
}

You can learn more about extending activity here.

@roblav96

Thanks I will look at this tonight. I am new to typescript so i need to figure out how to get android recognized. but i should be able to figure that out.

I made a simple example repo here for deep-linking on iOS and Android with Nativescript & Angular:
https://github.com/ddfreiling/tns-ng-deeplink

@ddfreiling Very nice, the OnRouteToURL stuff is what I was looking for.

@mrdeleon Updated my example, it wasn't working when starting the app with a deeplink. That works now.

@ddfreiling just finished updating my project everything looks to be working. Thanks for the example.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lscown picture lscown  路  163Comments

tjvantoll picture tjvantoll  路  58Comments

morningrat picture morningrat  路  67Comments

dbbk picture dbbk  路  54Comments

NickIliev picture NickIliev  路  58Comments