My GeofenceTransitionsIntentService does not send any notifications when a geofence transition occurs

2.5k views Asked by At

I am trying to implement a feature for adding and monitoring geofences and I thought that, this tutorial is the best one to start with.

I have done everything like they disribed, and it works but not like I would like to. I have noticed that geofence transistions are not tracked, what I actually want to say is when I enter the geofence, nothing happens but when I start the app inside of the geofence I get some response. I have also noticed that none of the methods from my GeofenceTransitionsIntentService are executed what actually means that geofence transition details will never be broadcasted to the MainActivity.

To be honest, this is the first time I am trying to implement this kind of feature so I really do not know what could be the cause of this issue. If you have any Idea, please respond, I will appreciate it.

This is what I have done so far

My AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.at.tuwien.hci.hciss2015">

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <!-- activities -->
    <service android:name="at.at.tuwien.hci.hciss2015.util.GeofenceTransitionsIntentService" />
</application>

My GeofenceTransitionsIntentService.java

public class GeofenceTransitionsIntentService extends IntentService {

protected static final String TAG = GeofenceTransitionsIntentService.class.getSimpleName();

public GeofenceTransitionsIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.e(TAG, "init GeofenceTransitionsIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Log.e(TAG, "geofencing event error");
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
            this,
            geofenceTransition,
            triggeringGeofences
        );
        Log.e(TAG, geofenceTransitionDetails);
        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        Log.e(TAG, "geofence transition invalid type");
    }
}

private String getGeofenceTransitionDetails(
        Context context,
        int geofenceTransition,
        List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList triggeringGeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences) {
        triggeringGeofencesIdsList.add(geofence.getRequestId());
    }
    String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}

private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

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

    // 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(this);

    // Define the notification settings.
    builder.setContentTitle(notificationDetails)
            .setContentText("Click notification to return to app")
            .setContentIntent(notificationPendingIntent);

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

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

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

private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "transition - enter";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "transition - exit";
        default:
            return "unknown transition";
    }
}
}

My MainActivity

public class MainActivity extends FragmentActivity implements
ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> {

protected GoogleApiClient mGoogleApiClient;

protected ArrayList<Geofence> mGeofenceList;

private PendingIntent mGeofencePendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
....

    mGeofenceList = new ArrayList<Geofence>();

    mGeofencePendingIntent = null;

    populateGeofenceList();

    buildGoogleApiClient();
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public void populateGeofenceList() {
    mGeofenceList.add(new Geofence.Builder()
            .setRequestId("1")
            .setCircularRegion(48.178454, 16.369699, 10)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build());

    mGeofenceList.add(new Geofence.Builder()
            .setRequestId("2")
            .setCircularRegion(48.17755, 16.369114, 10)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build());
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();

    LocationServices.GeofencingApi.removeGeofences(
        mGoogleApiClient,
        getGeofencePendingIntent()
    ).setResultCallback(this);

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    Location myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    initCamera(myLocation);

    LocationServices.GeofencingApi.addGeofences(
        mGoogleApiClient,
        getGeofencingRequest(),
        getGeofencePendingIntent()
    ).setResultCallback(this);
}

@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}

@Override
public void onResult(Status status) {
    if (status.isSuccess()) {

        Toast.makeText(this, "status: " + status.getStatus(),Toast.LENGTH_SHORT).show();
    } else {
        Log.e(TAG, "something is wrong");
    }
}

}
0

There are 0 answers