user3471066 user3471066 - 1 year ago 109
Android Question

Location Based Push Notifications For Android

Is there anyways in sending a location based push notification for android devices with out using a third party push notification service such as Parse? I would like to send push notification to my users without the annoyance of getting a notification that doesn't relate to that specific user because they are not in a certain area. Also, I could get the users location based on a time interval but I would rather do it a different way then that, if possible.

Answer Source

Yes, this is entirely possible, as long as I'm correctly interpreting what you are asking.

To accomplish this, you would send the GCM push notification to all of your users (unless you had a way, server-side of filtering some of them out). Then in your application, instead of just creating a Notification and passing it to the notification manager, you would first use the LocationManager (or the newer LocationServices API) to determine if the user is within the proper location first, and then just discard the GCM notification if they're not.

You'll need to take care of several things in order to do this:

  • Your AndroidManifest.xml will require several permission changes, both for the GCM changes, and for the Location access:

    <!-- Needed for processing notifications -->
    <permission android:name="com.trover.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.trover.permission.C2D_MESSAGE" />
    <uses-permission android:name="" />
    <!--  Needed for Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • You'll also need to set up a Notification Receiver in the <application> section of the manifest:

    <receiver android:name="com.myappname.NotificationReceiver" android:permission="">
            <action android:name="" />
            <category android:name="com.myappname" />
            <action android:name="" />
            <category android:name="com.myappname" />
  • In addition, you'll need write your NotificationReceiver java class, and override the onReceive function:

    public class NotificationReceiver extends BroadcastReceiver {
        public void onReceive(final Context context, final Intent intent) {
            if ("".equals(intent.getAction())) {
                handleRegistration(context, intent); // you'll have to write this function
            } else if ("".equals(intent.getAction())) {
                // the handle message function will need to check the user's current location using the location API you choose, and then create the proper Notification if necessary.
                handleMessage(context, intent);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download