Build Location Triggered Notifications with GeoSpark and Firebase

Jothi Priyadharshan
Jothi Priyadharshan
Co-founder & CTO
February 15, 2019

Location based notifications are the next big thing. Currently, marketers can target mobile users based on their IP address location, GPS location, and more. Location-based marketing is still in its infancy, but the technology is developing quickly.

How it Works

At the heart of Location Triggered Notifications are geofences. A geofence is a virtual fence that’s placed around a real-world location. They can range in size from very small (encircling a store) to very large (covering an entire city). Most often, geofences are placed around specific buildings or other points of interest.

GeoSpark supports unlimited geofences and you can also use our APIs to quickly upload locations. Once you’ve added at least one geofence to GeoSpark, you can start building location-based mobile marketing campaigns.

In this post, we will see how GeoSpark and Firebase come together to deliver this.

Building a Location Triggered Notifications with GeoSpark is Easy

Implementation overview

We would need a mobile application which has GeoSpark SDK integrated with it and can receive the location based notifications. This application uses the Firebase and GeoSpark Android SDKs. More specifically, this is what the location triggered notifications flow in the application looks like:

  1. User logs in to the application, using Firebase Authentication.
  2. On successful authentication, application gets or creates a user object on GeoSpark.
  3. Geofences are created using GeoSpark Dashboard or API.
  4. Campaigns are created in Firebase and stored in Firebase Realtime Database with Geofence Id.
  5. The application starts tracking with GeoSpark, and triggers  an event whenever the user enters or exits a geofence. The events are sent to the your firebase webhook url.
  6. The Firebase receives the event and then fetches the campaign details from Firebase Realtime Database.
  7. Finally notification is sent the customer application using the Firebase Cloud Messaging.

Implementing a location based notification involves a consumer app (which is being tracked) and the server. The example implementation uses GeoSpark Android SDK to illustrate a common implementation.

Let’s look at how these components are integrated, starting with creating a project in Firebase.

Firebase

Create a firebase project.

1. Go to Firebase

2. Login with a google account or create an account to login if you don’t have one.

3. Go to console.

4.   Add project, give it a name and click on CREATE PROJECT

Once you have created the project, you will be directed to the firebase console.

Next process; We should click the “Add Firebase to your Android App” and we enter the project package name. Google account login process needs to SHA1. We have to add SHA1 our project. After that, we should download google-services.json file and which has to be added into your project app folder. Finally, we should select Authentication on left side and we have to activate Email/Password and Google section.

Create a Firebase Realtime Database.

Create database in test mode that anyone with your database reference will be able to read or write to your database

Switch to Realtime Database.

GeoSpark Dashboard

  1. Create an account with GeoSpark here.
  2. Create a new project and add android application with the correct project package name.
  3. Get your Publishable Key from application settings page.

For the Android Application

  1. Pull the android application code from Github.
  2. Update the package name and add 'google-services.json' into the project app folder.
  3. Add your GeoSpark Publishable Key in the 'LoginActivity.java' file.

🎉 Congratulations, you have created your android application.

For the Backend Server

Make sure you have Node.js and npm installed and then install the Firebase CLI via npm:

npm install -g firebase-tools

This installs the globally available firebase command.

If the command fails, you may need to change npm permissions.

Pull the backend code from Github.

Run firebase login to log in and authenticate the firebase tool.

firebase login

Allow Firebase to collect anonymous CLI usage and error reporting information?

Yes

This will open your browser for you to choose an account

Go to functions folder.

cd functions

Run the following command on your terminal.

firebase deploy

The URL generated  will be something like below.

Add Geofence: https://us-central1-XXXXXXXX.cloudfunctions.net/addgeofence

Trigger Notification(Webhook URL): https://us-central1-XXXXXX.cloudfunctions.net/triggernotification

🎉 Congratulations, you have created your backend.

Now the setup is done and the below items are ready.

  1. Android Application
  2. Backend Server
  3. GeoSpark Dashboard

Let's do some magic 🧙‍♀️

Open the android application to enter your email id and password to create a register a user.

This will create users in your Firebase Realtime Database linking the user_id from GeoSpark as geospark_token.

Now, go to GeoSpark Dashboard and create Geofence and get the geofence id.

Use this geofence id with your Firebase backend endpoint 'addgeofence' to create a geofence with title and message which will be later displayed as notification to the user.

Endpoint URL:

https://us-central1-XXXXXXXX.cloudfunctions.net/addgeofence

Header:

Content-Type: application/json

Body:

Body: { "geofence_id": "5c63b80f9533a3390b24c59b", "gtitle": "GeoSpark HQ", "gmessage": "Welcome to GeoSpark HQ, GeoSpark is an AI driven location tracking platform with 90% less battery drain for your iOS and Android Apps." }

This will create geofences in your Firebase Realtime Database linking the geofence_id from GeoSpark.

Add your webhook url from firebase to application settings in GeoSpark Dashboard.

🎉 Congratulations, you have created a geofence and enabled webhook integrations.

When any of the users enter the geofence, they will receive the geofence message as notification.

Extras

This blog post contains only the example application using android and firebase where the users are authenticated via firebase and then updated into firebase realtime database along with Geospark user id for future identification. The addgeofence endpoint is to update geofences into firebase realtime database along with  geofence id from GeoSpark.

You can make use of below products from GeoSpark to build similar application or integrate GeoSpark into your existing application.

  1. GeoSpark SDK for iOS and Android
  2. GeoSpark APIs
  3. Trips
  4. Webhooks
  5. Insights

Try us out

How have you built your tracking experience with GeoSpark? We are always looking to improve ours and would love to hear from you. We offer our tracking experience through a quick integration experience to developers. There are open-source example application in our Github page.

Unlock Location Technology

Product
Jothi Priyadharshan
Jothi Priyadharshan
Co-founder & CTO
February 15, 2019