Deep link into app (specific activity) by NFC tag

5.3k views Asked by At

I'm developing an Android app that needs a deep link with an NFC tag.

Here you can see my intent filter for the activity:

<activity
    android:name=".ui.schedule.ScheduleActivity"
    android:parentActivityName=".ui.home.HomeActivity">

    <intent-filter android:label="testDeepLink">
        <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.testdeeplink.com"
            android:pathPrefix="/schedule"/>

    </intent-filter>

</activity>

Now when I start this command in adb the application starts with the correct activity (ScheduleActivity):

adb shell am start -W -a android.intent.action.VIEW -d "http://www.testdeeplink.com/schedule?stop_id=1" com.exmemple.android

But when I encode the URL on an NFC tag, scanning that tag just starts the web browser of my phone. What am I missing to start the activity with the NFC tag?

URL encoded on the tag: "http://www.testdeeplink.com/schedule?stop_id=1"

1

There are 1 answers

3
Michael Roland On BEST ANSWER

You are missing to put an NFC intent filter in your manifest. URLs on NFC tags won't trigger an intent action VIEW. Instead, they will be sent to activities with the intent action NDEF_DISCOVERED. Consequently, you can receive such an NFC intent by putting an additional intent filter for action NDEF_DISCOVERED in your manifest:

<activity
    android:name=".ui.schedule.ScheduleActivity"
    android:parentActivityName=".ui.home.HomeActivity">

    <intent-filter android:label="testDeepLink">
        <action android:name="android.intent.action.VIEW" />
        ...
    </intent-filter>
    <intent-filter android:label="testDeepLinkNFC">
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http"
              android:host="www.testdeeplink.com"
              android:pathPrefix="/schedule" />
    </intent-filter>

Note that there seem to be some (unconfirmed?) issues with some devices running Android 6.0+ where the browser seems to hijack URLs from NFC tags despite correct NDEF intent filters. I did not experience this myself so far so I could not further investigate this though.