Android firebase app indexing example

App indexing make our mobile application searchable in google application. This functionality will work only if we have a website along with the mobile application. You can use app indexing if you have some common content in mobile app as well as website.

If you find the tutorial useful please subscribe to our YouTube channel

Steps

  1. Create new project in android studio.
  2. Configure firebase project.
  3. Having firebase app indexing dependency in build.gradle file
  4. Add intent filters to AndroidManifest.xml
  5. Handle intent data in receiving activity.
  6. Test app indexing functionality

Note – This feature is implemented on Codesfor Mobile application. You can test it by downloading app from playstore which will give you above output. You can find all our tutorials,interview questions and answers in the mobile application As well as there is a free test taking platfrom where you can test your android knowledge.

Playstore Link

1.Create new project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

2.Configuring firebase application

In this step you need to configure a firebase application . Please refer this tutorial(Firebase) which will help you to create new project in firebase. If you already have a firebase project for your application skip this step.

3.Have firebase app indexing dependency in build.gradle file

If you are familiar with implementing any firebase functionality you will be knowing that we need to compile respective libraries of firebase . For compiling app indexing library you need to add the following dependency to the apps build.gradle file

compile 'com.google.firebase:firebase-appindexing:12.0.0'

4.Add intent filters to AndroidManifest.xml file

In this step we need to add some code to the AndroidManifest.xml file. We need add one intent filter to the activity which is suppose to open when user clicks on search result link of google. All you need to do is to add the following code inside your activity tag.

<intent-filter
android:autoVerify="true"
android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="codesfor.in"
android:scheme="http" />
</intent-filter>

In the above code you need to replace the scheme and host with your url information. So when somebody find any link in the search result starting with the above host name and  this particular user has installed your application then when he clicks on the link instead of browser it will directly open the application with the above activity.

5.Handle intent data in receiving activity

In this step we will learn how to handle the intent data when the respective activity open. For doing the same you need to have the following function in your activity and the function has to be call in the onCreate function of the activity.

protected void onNewIntent(Intent intent) {
String action = intent.getAction();
String data = intent.getDataString();
if (Intent.ACTION_VIEW.equals(action) && data != null) {
//do your action
}
}

Now in the above function you can get the entire url in which the user has clicked in the data string. From the url you can identity what the user has required and you can handle the data based on that info. Suppose the functionality of your application is to open a webview with the incoming url you can use the following code.

protected void onNewIntent(Intent intent) {
 String action = intent.getAction();
 String data = intent.getDataString();
 if (Intent.ACTION_VIEW.equals(action) && data != null) {
your-webview.loadUrl(data);

}
}

Or you can split the url string with “/” symbol and find the end url string, may be an id of a product , suppose if it is an e commerce application , you can open the activity by passing that product id which will display particular product. Now just add the following line in your onCreate activity for calling new intent

onNewIntent(getIntent());

6.Test your app indexing implementation

Now after completing the above steps you can test the implementation by executing the following command in your terminal. Before executing the terminal you need to run the application first. After triggering the command it will open your defined activity directly. Replace the website url and application package name with yours in the following code.

adb shell am start -a android.intent.action.VIEW -d "http://codesfor.in/android-push-notification-using-firebase-cloud-messaging-fcm-and-php/" com.codesfor.app

If your website is already crawled by google you can do live test also. For doing that just open the google app and search for any link of your website and on the results click on the link , it will open the activity in your android application.

Note – This feature is implemented on Codesfor Mobile application. You can test it by downloading app from playstore which will give you above output. You can find all our tutorials,interview questions and answers in the mobile application As well as there is a free test taking platfrom where you can test your android knowledge.

Playstore Link

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *