i know question asked, tried many suggested solutions doesn't work.
my code geofence works when app running, once app killed notification doesn't appear anymore when broadcastreceiver
you can find here code associated activity, btn_reservation button starts adding geofence.
public class detailsactivity extends appcompatactivity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, resultcallback<status> { public static final long geofence_expiration_in_milliseconds = 12 * 60 * 60 * 1000; public static final float geofence_radius_in_meters = 500 private list<geofence> mgeofencelist; private googleapiclient mgoogleapiclient; private button btn_reservation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_details); btn_reservation = (button) findviewbyid(r.id.reserver); mgeofencelist = new arraylist<geofence>(); mgeofencelist.add(new geofence.builder() .setrequestid("1") .setcircularregion( mapsactivity.getlatitude(), mapsactivity.getlongitude(), geofence_radius_in_meters ) .setexpirationduration(geofence_expiration_in_milliseconds) .settransitiontypes(geofence.geofence_transition_enter) .build()); // create instance of googleapiclient. if (mgoogleapiclient == null) { mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); } btn_reservation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!mgoogleapiclient.isconnected()) { toast.maketext(getapplicationcontext(), "google api client not connected!", toast.length_short).show(); return; } try { locationservices.geofencingapi.addgeofences( mgoogleapiclient, getgeofencingrequest(), getgeofencependingintent() ).setresultcallback(detailsactivity.this); // result processed in onresult(). } catch (securityexception securityexception) { // catch exception generated if app not use access_fine_location permission. toast.maketext(getapplicationcontext(), "access_fine_location permission unused ", toast.length_short).show(); } } }); } private geofencingrequest getgeofencingrequest() { geofencingrequest.builder builder = new geofencingrequest.builder(); builder.setinitialtrigger(geofencingrequest.initial_trigger_enter); builder.addgeofences(mgeofencelist); return builder.build(); } private pendingintent getgeofencependingintent() { intent intent = new intent("action_receive_geofence"); return pendingintent.getbroadcast( getapplicationcontext(), 0, intent, pendingintent.flag_update_current); } @override protected void onstart() { super.onstart(); if (!mgoogleapiclient.isconnecting() || !mgoogleapiclient.isconnected()) { mgoogleapiclient.connect(); } } @override protected void onstop() { super.onstop(); if (mgoogleapiclient.isconnecting() || mgoogleapiclient.isconnected()) { mgoogleapiclient.disconnect(); } } @override public void onconnectionfailed(connectionresult connectionresult) { } @override public void onconnected(bundle bundle) { if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { // todo: consider calling // activitycompat#requestpermissions // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activitycompat#requestpermissions more details. return; } location mlastlocation = locationservices.fusedlocationapi.getlastlocation( mgoogleapiclient); if (mlastlocation != null) { system.out.println("my lat " +string.valueof(mlastlocation.getlatitude())); system.out.println("my lon " +string.valueof(mlastlocation.getlongitude())); } } @override public void onconnectionsuspended(int i) { mgoogleapiclient.connect(); } @override public void onresult(status status) { if (status.issuccess()) { system.out.println("geofences added"); } else { system.out.println(status.getstatuscode()); } }
and geofencereceiver.java
public class geofencereceiver extends broadcastreceiver { context context; intent broadcastintent = new intent(); @override public void onreceive(context context, intent intent) { this.context = context; broadcastintent.addcategory(geofenceutils.category_location_services); geofencingevent event = geofencingevent.fromintent(intent); if (event.haserror()) { handleerror(intent); } else { handleenterexit(intent); } } private void handleerror(intent intent){ geofencingevent event = geofencingevent.fromintent(intent); // error code int errorcode = event.geterrorcode(); // error message string errormessage = locationserviceerrormessages.geterrorstring( context, errorcode); // log error log.e(geofenceutils.apptag, "geofence_transition_error_detail"+ errormessage); // set action , error message broadcast intent broadcastintent .setaction(geofenceutils.action_geofence_error) .putextra(geofenceutils.extra_geofence_status, errormessage); // broadcast error *locally* other components in app localbroadcastmanager.getinstance(context).sendbroadcast( broadcastintent); } private void handleenterexit(intent intent) { geofencingevent event = geofencingevent.fromintent(intent); int transition = event.getgeofencetransition(); // test valid transition reported if ((transition == geofence.geofence_transition_enter) || (transition == geofence.geofence_transition_exit)) { // post notification list<geofence> geofences = event .gettriggeringgeofences(); string[] geofenceids = new string[geofences.size()]; string ids = textutils.join(geofenceutils.geofence_id_delimiter, geofenceids); int tran = geofence.geofence_transition_enter; string transitiontype = string.valueof(tran); // create intent broadcast app broadcastintent .setaction(geofenceutils.action_geofence_transition) .addcategory(geofenceutils.category_location_services); localbroadcastmanager.getinstance(context) .sendbroadcast(broadcastintent); // log transition type , message log.d(geofenceutils.apptag, transitiontype + ": " + ids); log.d(geofenceutils.apptag, context.getstring(r.string.geofence_transition_notification_text)); // in debug mode, log result log.d(geofenceutils.apptag, "transition"); // invalid transition reported } else { // log error log.e(geofenceutils.apptag, "geofence_transition_invalid_type" + transition); } sendnotification(string.valueof(geofence.geofence_transition_enter), "here"); } private void sendnotification(string transitiontype, string locationname) { // create explicit content intent starts main activity intent notificationintent = new intent(context, ticketactivity.class); // construct task stack taskstackbuilder stackbuilder = taskstackbuilder.create(context); // adds main activity task stack parent stackbuilder.addparentstack(ticketactivity.class); // push content intent onto stack stackbuilder.addnextintent(notificationintent); // pendingintent containing entire stack pendingintent notificationpendingintent = stackbuilder .getpendingintent(0, pendingintent.flag_update_current); // notification builder that's compatible platform versions // >= 4 notificationcompat.builder builder = new notificationcompat.builder( context); // set notification contents builder.setsmallicon(r.drawable.cast_ic_notification_0) .setcontenttitle(transitiontype + ": " + locationname) .setcontenttext( context.getstring(r.string.geofence_transition_notification_text)) .setcontentintent(notificationpendingintent); // instance of notification manager notificationmanager mnotificationmanager = (notificationmanager) context .getsystemservice(context.notification_service); // issue notification mnotificationmanager.notify(0, builder.build()); system.out.println("in radius"); } }
and receiver in manifest
<receiver android:name="tn.odc.noq.receiver.geofencereceiver" android:exported="false"> <intent-filter > <action android:name="tn.odc.noq.receiver.action_receive_geofence"/> <category android:name="tn.odc.noq.receiver" /> </intent-filter> </receiver>
work geofences scratch it's hard, i've been tried along time this, didn't getting stub. i've adopted use libs
here awesome libs:
ex:
geofencemodel mestalla = new geofencemodel.builder("id_mestalla") .settransition(geofence.geofence_transition_enter) .setlatitude(39.47453120000001) .setlongitude(-0.358065799999963) .setradius(500) .build();
rxgeofence.with(this) //context .dburl(geo_fire_db) //string .geofencesource(...) // rx.observable<list<place>> .locationsource(...) // rx.observable<latlng> .build() .subscribe(geofenceevent -> { // wanna event });
geofence geofence = new geofence.builder() .setrequestid("geofence") .setcircularregion(latitude, longitude, radius) .setexpirationduration(geofence.never_expire) .settransitiontypes(geofence.geofence_transition_enter | geofence.geofence_transition_exit) .build();
i indicate use libs there large number of developers working solve problems, , negotiations exception.
Comments
Post a Comment