Contents

Flutter and Android development with Kotlin

Widget cost estimator
Coding With Dhrumil
Tools
All articles→

Android Geofencing with Push Notifications: A Practical Guide

Learn how to implement Android geofencing with push notifications using FusedLocationProviderClient, GeofencingClient, and FCM. Includes best practices and pitfalls.

Map with geofence boundary and smartphone notification, illustrating Android geofencing with push notifications.
Map with geofence boundary and smartphone notification, illustrating Android geofencing with push notifications. · Foto: Pexels / Pixabay

Geofencing lets your app react when a user enters or leaves a geographic area. Combined with push notifications, you can deliver timely, location-aware messages that feel magical when they work and frustrating when they don't.

This guide walks through a reliable implementation using Android's GeofencingClient and Firebase Cloud Messaging (FCM). We'll cover setup, event handling, notification delivery, and the practical numbers you need to keep battery drain and latency in check.

Key points

  • Use GeofencingClient with a PendingIntent to a BroadcastReceiver for entry/exit events.
  • Keep geofences few (10–20) and radii large (300m+) to save battery.
  • Send push notifications via FCM from your backend after receiving geofence events.
  • Request background location permission and handle Android 10+ restrictions.
  • Test with mock locations and monitor battery with Battery Historian.

Why Geofencing + Push Notifications?

Geofencing alone can trigger local work, but push notifications let you reach users even when your app is not running. For example, a retail app can notify users about a sale when they walk near a store, or a transit app can alert them when they arrive at their stop. The combination is powerful because the geofence event wakes your app or a background service, which then requests a notification from your server.

However, geofencing is not a precision tool. Expect entry/exit detection to have a latency of 1–5 minutes in typical urban conditions, and longer in rural areas. Battery impact varies: a well-tuned geofence adds roughly 1–3% battery drain per hour of active tracking, but poor configurations can double that. Design your notifications to be tolerant of delays and avoid spamming users.

  • Geofence radius: 100–500 meters for urban, up to 1 km for rural
  • Response latency: typically 1–5 minutes
  • Battery impact: 1–3% per hour with balanced settings
  • Always request the minimum necessary permissions
50m4.5100m3.5300m2500m1.21000m0.4
Battery drain by geofence radius (% per hour)

Setting Up Geofences with GeofencingClient

Start by adding the Google Play services location dependency. Then create a GeofencingClient instance and build Geofence objects with a request ID, circular region, and transition types (GEOFENCE_TRANSITION_ENTER, EXIT, or DWELL). Use a PendingIntent that points to a BroadcastReceiver or Service to handle the event.

Register geofences with geofencingClient.addGeofences(). Keep the number of active geofences per user low—Google Play services limits you to 100 per app per device. For most apps, 10–20 well-chosen geofences are enough. Set the initial trigger to INITIAL_TRIGGER_ENTER so you get an event if the user is already inside the fence when it's added.

Always handle the result callbacks. Common failures include missing permissions, too many geofences, or background location restrictions on Android 10+. Log the status codes to diagnose issues quickly.

  • Use a unique request ID for each geofence
  • Set expiration to avoid stale geofences (e.g., 7 days)
  • Limit to 10–20 geofences per user for battery
  • Handle add/remove failures gracefully
Geofence radius vs. battery impact and latency
Radius (meters)Typical latencyBattery impact per hourBest for
50–1001–2 minutes3–5%Precise alerts (e.g., store entry)
100–3002–4 minutes2–3%Neighborhood notifications
300–5003–5 minutes1–2%City-level campaigns
500–10005–10 minutes0.5–1%Regional reminders
1000+10+ minutes<0.5%Low-priority, broad areas

Handling Geofence Transitions Reliably

Geofence transitions are delivered to your BroadcastReceiver (or Service) via an Intent. Extract the GeofencingEvent and check its error code. If successful, get the transition type and the triggering geofences. You may receive multiple transitions at once, so iterate through the list.

Because geofencing can trigger while the device is asleep, keep your receiver lightweight: do not perform network calls or heavy processing there. Instead, enqueue work with WorkManager or start a foreground service if you need immediate action. For push notifications, you'll typically send a request to your server, which then uses FCM to deliver the notification.

Remember that Android may kill your process at any time. Use a WakeLock only if absolutely necessary, and release it promptly. Test with battery saver and doze mode enabled to ensure your notifications still arrive.

  • Use goAsync() in BroadcastReceiver for short async work
  • Prefer WorkManager for deferrable tasks
  • Avoid network calls directly in the receiver
  • Test with Doze and battery saver on

Sending Push Notifications via FCM

When your app receives a geofence event, it can call your backend with the event details. Your backend then sends a push notification through Firebase Cloud Messaging (FCM) to the specific device token. This decouples notification content from the app and lets you personalize messages.

On the client, use FirebaseMessagingService to handle incoming messages. For notification messages, FCM automatically displays them when the app is in the background. For data messages, you must build and show the notification yourself using NotificationCompat. Use notification channels (required on Android 8.0+) and set appropriate importance.

To avoid overwhelming users, implement server-side throttling: send at most one geofence notification per user per hour, and respect user preferences. Also, handle token refresh—upload the new token to your server whenever onNewToken is called.

  • Use data messages for custom handling
  • Create notification channels with descriptive names
  • Throttle notifications to 1 per hour per user
  • Handle FCM token refresh

Permissions and Background Location

Geofencing requires ACCESS_FINE_LOCATION. On Android 10 (API 29) and above, you also need ACCESS_BACKGROUND_LOCATION to receive geofence events when the app is in the background. Requesting background location is a sensitive flow: you must explain why you need it and may be required to direct users to settings.

Starting with Android 11, the system limits background location access if the user hasn't used your app recently. Design your geofences to be resilient: use the GeofencingClient's ability to re-register on boot via a BOOT_COMPLETED receiver. Also, check for Google Play services availability, as geofencing depends on it.

If your app targets Android 12+, be aware of the approximate location option. Users can grant only approximate location, which reduces geofence accuracy. Always request precise location for geofencing and provide a clear rationale.

  • Request ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION
  • Show a rationale before requesting background location
  • Re-register geofences on device boot
  • Check Google Play services availability

Testing and Debugging Geofences

Testing geofences is tricky because you can't easily simulate movement. Use the emulator's location features to set a route or send mock locations. Alternatively, use a test geofence with a small radius (e.g., 20 meters) and manually toggle your location in the emulator.

Log all geofence events with timestamps to understand latency. On physical devices, use the 'Mock Location' app to simulate movement. Remember that geofences are removed when the app is uninstalled or data is cleared, so re-register after updates.

For push notifications, use the FCM console to send test messages. Monitor delivery rates and user engagement to refine your geofence strategy. If you're also working with local data, see our Room migration checklist to avoid data loss during updates.

  • Use emulator routes or mock location apps
  • Log entry/exit events with timestamps
  • Test with app in background and killed
  • Verify notification channels and permissions

Performance and Battery Best Practices

Geofencing is a background service, so battery efficiency matters. Set the geofence's notification responsiveness to a higher value (e.g., 5 minutes) if you don't need immediate alerts. Use a larger radius to reduce wake-ups. Combine geofences with activity recognition to avoid unnecessary checks when the user is stationary.

Monitor battery usage with Android's Battery Historian. A typical geofence setup should consume less than 5% battery per day for moderate use. If you see higher, review your geofence count, radius, and responsiveness. Also, consider using the Fused Location Provider's batching to reduce wake-ups.

Finally, always provide a way for users to disable geofencing. Respecting user choice improves retention and reduces complaints. If you're building a Flutter app, our guide on StatelessWidget vs StatefulWidget can help you structure your UI efficiently.

  • Set responsiveness to 5 minutes for non-urgent alerts
  • Use larger radii (500m+) to reduce triggers
  • Monitor with Battery Historian
  • Allow users to opt out easily
Dhrumil Patel

Editor and Flutter contributor. Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.

Flutter Screen Widget and Cost Estimator

Related articles

Cookie preferences

Required for the site to function, including remembering your cookie choice. Always on.

Not used on this site.

Not used on this site.