Android Geofence – Entry and Exit

Geofencing allows you to send notifications to your users in Android. when they are in the vicinity of, for example, your store. By doing so, you only provide your users with information relevant to them and increase your app usage, foot-traffic and conversion.Using Android Geofencing ,we are identify Exit and Entry point of users.

A Geofence is a virtual perimeter around a location. You can create Geofences of 50 to 50.000 meters radius to send location-based notifications to users approaching, for example, your stores, coffee shops or events.

geofence

Create Geofences

Creating Geofences in the Plot Dashboard is as easy as riding your bike. You can search for a place or address and then set the name and radius of the Geofence.

Creating Geofences on a map gives you perfect control of where your users will receive your Location Based Notifications.

  public Geofence toGeofence() {
        // Build a new Geofence object
        return new Geofence.Builder()
                       .setRequestId(Long.toString(id))
                       .setTransitionTypes(transitionType)
                       .setCircularRegion(latitude, longitude, radius)
                       .setExpirationDuration(expirationDuration)
                       .build();
    }

Geofence Enter

When user exit the geofence , user getting trigger and showing that you are entering geofence.

public class ReceiveTransitionsIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
int transition = LocationClient.getGeofenceTransition(intent);
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)){ if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) { //bussiness logic AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVice); Intent broadcast_intent = new Intent(this, GeofencigNotificationReciever.class); broadcast_intent.putExtra("EventId", eventId); //data to pass broadcast_intent.putExtra("TransitionType", transitionType); PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, broadcast_intent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent); } } } }

 

Geofence Exit

When user exit the geofence,user getting trigger and showing that you are exit geofence.

public class ReceiveTransitionsIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
int transition = LocationClient.getGeofenceTransition(intent);
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)){
if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
//Bussiness logic
} } } }

 

Create Notifications

After creating a Geofence you can set a notification attached to that Geofence. It is possible to set a specific notification for every Geofence you have.

You can easily define where the notification should lead to once a user opens it: a website, a page in your app or a custom code, everything is possible!

private void sendNotification(String transitionType, String id, Context context){		
DatabaseHandler provider = new DatabaseHandler(context);	
SimpleGeofence item = provider.getGeofence(id);
String address = item == null ? "" : item.getName();		
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(context, NotificationResponseActivity.class);
notificationIntent.putExtra("EventId", id);		
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(NotificationResponseActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("welcome")
.setContentText("You've arrived at your appointment").setContentIntent(notificationPendingIntent);		
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);		
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);		
NotificationCompat.BigTextStyle inboxStyle =
new NotificationCompat.BigTextStyle();		
inboxStyle.bigText("You've arrived at your appointment.");		
builder.setStyle(inboxStyle);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}

 

logo

 

Value creation through technology & innovation

Patterns7 Technologies PVT LTD is software Development Company based in United Kingdom & India.The world is changing so fast that it is becoming increasingly difficult to plan ahead. We use Innovative Solutions to help businesses to get ready for the future by providing Innovative Technology Services. With deep and wide experience in providing end-to-end solution we are helping our clients to deliver Value.

Currently we are working in IOT, CRM, Healthcare and Manufacturing industries to create value through information flow in realtime.

Please feel free to contact us on info@patterns7tech.com

Submit a Comment

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