0

I am developing an android application related to geofencing and I tried Broadcast receiver to get a notification when entering into geofence and I will work only when the application is running and then I tried Service to get a notification but now no notification is coming when entering into geofence.

Here is the code

**

package com.example.fypautosilenceapp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
public class GeofenceService extends Service {
    private static final String TAG = "GeofenceBroadcastRecive";
    AudioManager audioManager;

    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        NotificationHelper notificationHelper=new NotificationHelper(getApplicationContext());
        GeofencingEvent geofencingEvent=GeofencingEvent.fromIntent(intent);
        if(geofencingEvent.hasError()){
            Toast.makeText(getApplicationContext(), "Geofence has error", Toast.LENGTH_SHORT).show();
            Log.d(TAG,"OnRecive:Error recive geofence event...");
        }
        List<Geofence> geofenceslist=geofencingEvent.getTriggeringGeofences();
        //Location geofencelocation=geofencingEvent.getTriggeringLocation();
        for (Geofence geofence:geofenceslist){
            Log.d(TAG,"OnRecive:"+geofence.getRequestId());
        }
        int transtiontype=geofencingEvent.getGeofenceTransition();
        switch (transtiontype){
            case Geofence.GEOFENCE_TRANSITION_ENTER:
             //   Toast.makeText(getApplicationContext(),"Enter geofence",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Enter geofence","",MapsActivity.class);
              //  audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
             //   Toast.makeText(getApplicationContext(),"Exit geofence",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Exit geofence","",MapsActivity.class);
               // audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
            //    Toast.makeText(getApplicationContext(),"Enter Dwell",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Enter Dewell","",MapsActivity.class);
           //     audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
        }
        return null;
    }
}

**

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51

0 Answers0