Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive the Geofence in app background . #150

Open
mssaravanan opened this issue Jul 8, 2016 · 1 comment
Open

Receive the Geofence in app background . #150

mssaravanan opened this issue Jul 8, 2016 · 1 comment

Comments

@mssaravanan
Copy link

mssaravanan commented Jul 8, 2016

Pls give example code for receive the geofence from background .. I was follow your readme steps to add the broad cast receiver , but not get any update from geofence's . I am already using google sample app for geofence its working fine with my lat, lon .

I have register the brodcast receiver in manifest ...

<receiver android:name="com.assistanz.geofencereceiver.GeoFenceReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.QUICKBOOT_POWERON"/> </intent-filter> </receiver>

And my broadcast receiver is ,
`public class GeoFenceReceiver extends BroadcastReceiver {
@OverRide
public void onReceive(Context context, Intent intent) {
if (BROADCAST_INTENT_ACTION.equals(intent.getAction())) {
final int transitionType = intent.getIntExtra(TRANSITION_EXTRA_ID, -1);
final List geofencingIds = intent.getStringArrayListExtra(GEOFENCES_EXTRA_ID);
Location location = intent.getParcelableExtra(LOCATION_EXTRA_ID);
Toast.makeText(context, getTransitionNameFromType(transitionType), Toast.LENGTH_SHORT).show();
sendNotification(getTransitionNameFromType(transitionType),context);
for (final String geofenceId : geofencingIds) {

        }
    }

}


private String getTransitionNameFromType(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "enter";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "exit";
        default:
            return "dwell";
    }
}

/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MainActivity.
 */
private void sendNotification(String notificationDetails,Context context) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(context, MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.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);

    // Define the notification settings.
    builder.setSmallIcon(R.drawable.ic_menu_share)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.ic_menu_slideshow))
            .setColor(Color.RED)
            .setContentTitle("iAttention")
            .setContentText(notificationDetails)
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

}`

@andro-jedi
Copy link

It seems Geofence not working at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants