Location Based Notifications for Android

Is there anyway when sending location-based location alerts for Android devices using a third-party push notification service like Parse? I would like to send a push notification to my users without the annoyance of receiving a notification that is not associated with this particular user, since they are not in a specific area. In addition, I could get the location of users based on a time interval, but I would prefer to do it differently than possible.

+5
source share
1 answer

Yes, it is quite possible if I correctly interpret what you ask.

To accomplish this, you will send a GCM push notification to all your users (unless you had a way, on the server side, filter out some of them). Then, in your application, instead of just creating a notification and passing it to the notification manager, you first need to use the LocationManager (or the new LocationServices API) to determine if the user is in the right place, and then simply drop the GCM if it is not.

To do this, you need to take care of a few things:

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

    <!-- Needed for processing notifications --> <permission android:name="com.myappname.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.myappname.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Needed for Location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  • You also need to configure the notification receiver in the <application> section of the manifest:

     <receiver android:name="com.myappname.NotificationReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.myappname" /> </intent-filter> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.myappname" /> </intent-filter> </receiver> 
  • In addition, you need to write your NotificationReceiver java code class and override the onReceive function:

     public class NotificationReceiver extends BroadcastReceiver { public void onReceive(final Context context, final Intent intent) { if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) { handleRegistration(context, intent); // you'll have to write this function } else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) { // the handle message function will need to check the user current location using the location API you choose, and then create the proper Notification if necessary. handleMessage(context, intent); } } 
+9
source

All Articles