Android Tutorial Using App Deeplinking in your app (based on json Config file)

In this tutorial i will show a way of using MobileDeepLinking in your app.

To use App deeplinking in your app to you need to setup your manifet to do so

Lets start with it:

Edit your manifest and add
B4X:
AddApplicationText(<activity
  android:name="org.mobiledeeplinking.android.MobileDeepLinking"
  android:theme="@android:style/Theme.NoDisplay"
  android:noHistory="true">
  <intent-filter>
    <data android:scheme="mydeep"/>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
  </intent-filter>
</activity>)
to it.
mydeep in this case is the deeplinking scheme i added for my tests...

To make use of the Activity org.mobiledeeplinking.android.MobileDeepLinking you need to use the attached B4A Library (MobileDeeplinking). See Example too

In this tutorial i will show you a way to get dynamic (not really as a file must be placed in assets. But you can update your app to get a new config file to use) deeplinking based on an json file which configures all deeplinks. You can see at the deeplinking lib as kind of a "router for your apps deeplinks".

assets/mobiledeeplinkingconfig.json
{
"logging": "true",
"defaultRoute": {
"class": "de.donmanfred.deeplinking.main"
},
"routes": {
"product/:productID/:customerID": {
"handlers": ["testHandler"],
"name": "Product",
"class": "de.donmanfred.deeplinking.main",
"routeParameters" : {
"productID" : {
"regex": "[0-9][0-9]?[0-9]?[0-9]?"
},
"customerID" : {
"regex": "[0-9][0-9]?[0-9]?[0-9]?"
}
}
},
"order/:eek:rderID": {
"handlers": ["testHandler"],
"name": "Product",
"class": "de.donmanfred.deeplinking.orders",
"routeParameters" : {
"orderID" : {
"regex": "[0-9a-zA-Z]{0,10}"
}
}
}
}
}
defaultRoute: If no route matches then this activity will be started. In this case the Main activity of the provided example. It´s like calling the url "mydeep://somewhat"

But if you use the url "mydeep://product/123/4567" this will call the main activity too but provides some kind of pairing the url to variables (kind of)

In this case the productID is set to 123 and the customerID is set to 4567. This is delegated/routed to the activity de.donmanfred.deeplinking.main

If you use the url "mydeep://order/ABCDEF" it will set the orderID to ABCDEF and call the Activity
de.donmanfred.deeplinking.orders (Activity orders in example)

For more informations on what you can do with the json please refer to the original sites documentation!


Main Activity
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim deeplink As MobileDeepLinking
    Dim lastIntent As Intent
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    deeplink.Initialize("","testHandler")
    'Dim json As String = File.ReadString(File.DirAssets,"config2.json")
    'deeplink.Configuration = json

    Dim Intent1 As Intent
  Intent1.Initialize(Intent1.ACTION_VIEW, "mydeep://order/ABCDEF") ' Will open Activity orders and the starting intent holds the orderID ABCDEF
  StartActivity(Intent1)
 

    'Dim Intent1 As Intent
    'Intent1.Initialize(Intent1.ACTION_VIEW, "mydeep://product/123/4567") ' Will open the main activity and the starting intent holds
    ' productID=123 and customerID=4567
    'StartActivity(Intent1)
End Sub

Sub Activity_Resume
    Dim i As Intent = Activity.GetStartingIntent
    If i <> Null Then
        If i <> lastIntent Then
            lastIntent = i
            Log("Action="&i.Action)
            If i.Action = "DeepLink" Then
                If i.HasExtra("productID") Then
                    Log("productID="&i.GetExtra("productID"))
                End If
                If i.HasExtra("customerID") Then
                    Log("customerID="&i.GetExtra("customerID"))
                End If
            End If
        End If       
    End If

End Sub


Activity orders
B4X:
Sub Activity_Resume
    Dim i As Intent = Activity.GetStartingIntent
    If i <> Null Then
        If i <> lastIntent Then
            lastIntent = i
            Log("Action="&i.Action)
            If i.Action = "DeepLink" Then
                If i.HasExtra("orderID") Then
                    Log("orderID="&i.GetExtra("orderID"))
                End If
            End If
        End If       
    End If

End Sub


If you want to donate for my work building the wrapper and this tutorial you can do it here:
 

Attachments

  • MobileDeeplinkingExample.zip
    8.4 KB · Views: 514
  • libMobileDeeplinkingV1.0.0.zip
    11.9 KB · Views: 556

Pendrush

Well-Known Member
Licensed User
Longtime User
When I try to test deep link in example attached to post #1 i get this error
loggingEnabled = true
intent = mydeep://order/ABCDEF
routeUsingUrl()
RouteStr=order/:eek:rderID
MobileDeepLinking: Route {orderID=ABCDEF}
MobileDeepLinking.handleRoute: options {"class":"de.donmanfred.deeplinking.orders","routeParameters":{"orderID":{"regex":"[0-9a-zA-Z]{0,10}"}},"name":"Product","handlers":["testHandler"]}
MobileDeepLinking: matching and handling route!
MobileDeepLinking: Error parsing JSON! org.json.JSONException: No value for name

Test is done with command taken from https://developers.google.com/app-indexing/android/test
adb shell am start -a android.intent.action.VIEW -d "mydeep://order/ABCDEF" de.donmanfred.deeplinking

and app refuse to start
 

DonManfred

Expert
Licensed User
Longtime User
it works for me

adb shell am start -a android.intent.action.VIEW -d "mydeep://order/ABCDEF" de.donmanfred.deeplinking
** Activity (orders) Pause, UserClosed = true **
intent = mydeep://order/ABCDEF
routeUsingUrl()
RouteStr=product/:productID/:customerID
MobileDeepLinking: Route null
RouteStr=order/:eek:rderID
MobileDeepLinking: Route {orderID=ABCDEF}
MobileDeepLinking.handleRoute: options {"handlers":["testHandler"],"name":"Product","class":"de.donmanfred.deeplinking.orders","routeParameters":{"orderID":{"regex":"[0-9a-zA-Z]{0,10}"}}}
MobileDeepLinking: Handler testHandler will be executed.
Handler:{orderID=ABCDEF}
afterroute...
** Activity (orders) Create, isFirst = false **
** Activity (orders) Resume **
Action=DeepLink
orderID=ABCDEF
** Activity (orders) Pause, UserClosed = false **
** Activity (orders) Resume **

Please note that the app must be installed and must be run once before you call this adb call
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Yes I know, install app, run app, close app, run adb shell... without success.
I have already implemented deeplinking without library, just want to test lib nothing more.
However, I'm unable to run example app with adb shell command.
Tested on genymotion (5.1.1/4.4.4) emulator, and on Samsung Galaxy Tab 3 device (4.4.2).
Same error in all tests.
App with "manual" implementation without this lib, run as expected on all tested devices.
 

fredo

Well-Known Member
Licensed User
Longtime User
Yes I know, install app, run app, close app, run adb shell... without success.
I have already implemented deeplinking without library, just want to test lib nothing more.
However, I'm unable to run example app with adb shell command.
Tested on genymotion (5.1.1/4.4.4) emulator, and on Samsung Galaxy Tab 3 device (4.4.2).
Same error in all tests.
App with "manual" implementation without this lib, run as expected on all tested devices.
@Pendrush, I'm interested in the manual implementation of "deeplinking".
Could you give me a push to the best practices or a small test-project?
 

johndb

Active Member
Licensed User
Longtime User

fredo

Well-Known Member
Licensed User
Longtime User

ashton293

Member
Licensed User
Longtime User
Hi Manfred,

I recently enquired regarding deep links through Firebase and you indicated your deeplinking is not done through Firebase.

I assume this is the method you are using.

I am trying to implement this approach however continue to receive errors. Can you please review the following and identify what I need to correct to get this process working.

Under Globals I have:
Dim deeplink As MobileDeepLinking
Dim lastIntent As Intent

Under Activity Create I have:
deeplink.Initialize("","testHandler")
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_VIEW, "abndeep://Main")
StartActivity(Intent1)

Under Activity Resume I have:

Dim i As Intent = Activity.GetStartingIntent
If i <> Null Then
If i <> lastIntent Then
lastIntent = i
Log("Action="&i.Action)
End If
End If

I have removed Has Extras as all I am doing is going to a module name in the app - in this case "Main" - there are another 10 or so modules in this app.


Under Android Manifest I have:
<activity android:name="org.mobiledeeplinking.android.MobileDeepLinking"
android:theme="@android:style/Theme.NoDisplay"
android:noHistory="true">
<intent-filter>
<data android:scheme="abndeep"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>

[As well as the full Firebase code that is required to run Firebase. I don't know if there are any software conflicts]

The config File contains:
{
"logging": "true",
"defaultRoute": {
"class": "b4a.packagename.main"
},
"routes": {
"main": {
"handlers": ["testHandler"],
"name": "Main",
"class": "b4a.packagename.main",
}
}
}

The Logcat does indicate there is a syntax error in the config file - it might be there but I can't see it.

Your assistance would be appreciated.

Thank you.
 

Attachments

  • LogCat for deeplinking issue.txt
    49.3 KB · Views: 484
Top