Geofencing for boost your digital campaign
It has become a chronic problem for me and my friends at the office at lunch time. We go to the mall to find a place to eat, but we spend 15–20 minutes just to decide which restaurant to eat. It caused we don’t have information that we can easily access to know which restaurants have discounts. So based on that background I often think it might be very helpful if the existing application has a feature that will provide notifications regarding promo information around my current location.
There is a technology that can be used to solve this problem, namely geofencing. Geofencing is a virtual radius built in an area. And there are 3 types of trigger:
1. Static Trigger, user device will trigger active event in fixed area.
2. Peer to peer trigger, a user device will trigger an active event on another user’s device.
3. Dynamic Trigger, the user device will trigger an active event in the data stream.
And for the case that I described at the beginning is an example of a static trigger. Because the location of the campaign is fixed and does not move. To implement geofencing for static triggers a Geofencing API is needed. The geofencing API is part of the geofence provided by the Google Maps SDK. This feature allows us to set a parameter in the form of a radius around the installed latitude and longitude while monitoring the location of devices that are within the radius. Then when it is within a radius that is monitored by geofencing api, the device will get a campaign notification.
1. To implement api geofencing, an Android SDK Map is required. Just like we use features from google maps we have to generate an api key and activate this service.
2. Next in your project, you can enter your api key in the value string as below or if you want to be more secure, enter the apikey in the NDK in C++ format, this will prevent your apikey from being read when the application is decompiled.
3. Because we are using a service from Google which is in the form of location. Required google service plugin needs to be added.
4. Furthermore, the library for the location service also needs to be added.
5. Next, ask for device permissions for the location. And to track device location, we can use network provider and gps provider. And both are provided by Fine Location and Coarse Location, so both need to be added to the manifest.
However, to use geofencing we need a location in foreground and background conditions. Imagine if the application only has permissions in foreground condition, when the device has entered the radius monitored by geofencing, the notification trigger will not be received by the user if the application is not being opened, to ensure the user gets a notification when not opening the application, permission for background location is required.
There are several things to note regarding the background location. For sdk level 27 or android version 8.1 and below when we have Fine Location and Coarse Location permissions, Background Location will automatically be obtained. But the background location will only be accessible a few times an hour so it can’t be tracked all the time.
On sdk level 28 or android 9 we don’t need to add background location permission in manifest file. Because the background location is already included along with the existing location permissions. The difference is only in the background location track that can be done all the time, not just a few times every hour.
On sdk level 29 or android 10, background location permission needs to be added to the manifest file, but the request process can be done simultaneously with the foreground location. So that the background permission definition only needs to be added to the permissions variable.
Pada level api 30 ke atas requesting background dan foreground terpisah. Teman — temen perlu menambahkan kondisi yang menghandle OS version pada android Q keatas (OS 11, OS 12, OS 13). Jika tidak ada sebuah kondisi spesifik yang menghandle kasus tersebut. Ketika kita coba compile app dengan existing code pada OS 11 ke atas dialog permission yang muncul hanya ada opsi untuk track foreground location like the picture below.
Create conditions for api level 30 and above then add background permission definition and request function. Then when we run to api level 11 and above a new dialog option will appear with 4 options.
6. After setting the location permissions, then implement the geofencing api. To communicate with geofencing we need the GeofencingClient() class. Create a variable of type GeofencingClient and initialize it with LocationService.geofencingclient() to be able to access the instance of geofencing.
There are 3 public functions that can provide the need to use geofencing services.
7. There are 3 most important attributes in geofencing (Lat, Lon, Radius), to determine the campaign point, latitude and longitude are needed. Next when the point is defined we need a definition of the radius which can be monitored by the Geofencing API. We can set this according to our own, but the distance suggested by geofencing api is 150-200 meters.
8. After the geofencing area is defined, then the transition type when the device is in the geofencing radius needs to be determined as well.
9. Actually, the use of this transition type can be adjusted according to needs, but to avoid unnecessary spam notifications. We can try using transition dwell. And add setLoiteringDelay attribute with millisecond format to make sure the minimum duration of user in the radius to activate the trigger.
10. To ensure the campaign is installed within the desired period duration, you can use the setExpiration attribute, then enter the value in millisecond format.
However, if you want the campaign to continue to be installed, just make NEVER_EXPIRE the value.
11. We already have a geofence setup, then make a geofencing request by making the geofence setup as previously created as a parameter of the public method addgefence().
At this stage, you must ensure that the initial geofencing event trigger is set. The transition type used in the geofence setup can be used as an initial trigger. To send a request to the Geofencing API, it takes a geofencingClient with 2 parameters, namely a geofencing request and a pending intent that contains the event to be launched. Create a pendingIntent containing a message that will be received by the receiver. Which later the event will be handled by the service.
12. The logic is that when the user device activates the geofencing trigger, the geofencing event will be launched and forwarded to the receiver which will then activate the service in the form of notifications. And the last, make sure the receiver and service have been initialized in the manifest and to activate the service add permission in the form of BIND_JOB_SERVICE.
13. Try to set the campaign and mock your current location around the campaign location. Check it, do you get the notification? Check the complete code.