Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pending Intent not triggering Intent Service in Geofencing

I am using an intent service to create a notification when user enters in the defined geofenced area.The problem is that when I first run the application it works fine and I am getting the pending-intent on my Intent Service, but after some days(2-3), I am not getting the required intent on the Intent Service.

I have no clue why it stopped working after some days. If I launch the application, it will start normally again but then stopped again after some days.

Here is my activity code --

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, ResultCallback, OnRequestPermissionsResultCallback {

GoogleApiClient mGoogleApiClient;
Location mGeoLocation;
Geofence mGeofence;

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();


    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 100);

    }
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mGeoLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    mGoogleApiClient.connect();
}

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


@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onClick(View v) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
   Toast.makeText(MainActivity.this,"",Toast.LENGTH_SHORT);
    Log.e("Here I am","Using geofencing in my mobile on 'onConnectionFailed'  of main activity");
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    if (mGeoLocation != null) {
        mGeofence = new Geofence.Builder()
                .setRequestId("Appstudioz")
                .setCircularRegion(mGeoLocation.getLatitude(), mGeoLocation.getLongitude(), 100)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
        mGoogleApiClient.connect();
        Intent intent = new Intent(this, MyIntentServiceGeoFencing.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofence(mGeofence);
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, builder.build(), pendingIntent).setResultCallback(this);


    }
}
@Override
public void onConnectionSuspended(int i) {
    Log.e("Here I am","Using geofencing in my mobile 'onConnectionSuspended' of main activity");
}

@Override
public void onResult(@NonNull Result result) {
    Log.e("Here I am","Using geofencing in my mobile 'onResult' of main activity");
}
}

And this is my Intent Service --

public class MyIntentServiceGeoFencing extends IntentService {
public MyIntentServiceGeoFencing() {
    super("MyIntentServiceGeoFencing");
}
@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        String message="";
        Log.e("Here I am","Using geofencing in my mobile 'In intent Service'");
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if(geofencingEvent.getGeofenceTransition()== Geofence.GEOFENCE_TRANSITION_ENTER)
        {
            message="Entering Appstudioz";
        }
        else if(geofencingEvent.getGeofenceTransition()== Geofence.GEOFENCE_TRANSITION_EXIT)
        {
            message="Exiting Appstudioz";
        }
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.cast_ic_notification_small_icon)
                        .setContentTitle("Geofence Notification")
                        .setContentText(message);

// Sets an ID for the notification
        int mNotificationId = 001;
// Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
        if(message.equals("Entering Appstudioz")) {
            ((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        }
        else
        {
            ((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

    }
}
}
like image 669
Akash Bisariya Avatar asked Apr 03 '17 07:04

Akash Bisariya


1 Answers

I have found my solution. Following are the reasons why the App was not getting Pending Intents according to the official google documentation -

1.The device is rebooted.
2.The app is uninstalled and re-installed.
3.The app's data is cleared.
4.Google Play services data is cleared.
5.The app has received a GEOFENCE_NOT_AVAILABLE alert.(When Android Location Provider(GPS) gets switched off)

You have to re-register the geofence after these events.

In my case device rebooting and location provider(GPS) getting switched off, were the reasons for not getting the pending intents.

like image 111
Akash Bisariya Avatar answered Oct 11 '22 01:10

Akash Bisariya