private async void OnClickSendRequest(object sender, System.EventArgs eventArgs)
        {
            string Tag       = "GeoFenceSendRequest";
            var    geofences = HMSGeofenceData.Geofences;

            if (geofences.Count == 0)
            {
                geoRequestData.Text = "No new request to add.";
                return;
            }
            if (requestList.Count == 0)
            {
                geoRequestData.Text = "No PendingIntent to send.";
                return;
            }
            if (requestList.Any(x => x.IsIDExist()))
            {
                geoRequestData.Text = "ID already exist, please remove and add it again.";
                return;
            }
            GeofenceRequest.Builder geofenceRequest = new GeofenceRequest.Builder();
            geofenceRequest.CreateGeofenceList(geofences);
            int intTrigger;

            if (int.TryParse(trigger.Text, out intTrigger))
            {
                geofenceRequest.SetInitConversions(intTrigger);
                log.Info(Tag, $"Trigger is {intTrigger}");
            }
            else
            {
                geofenceRequest.SetInitConversions(5);
                log.Info(Tag, "Trigger is 5");
            }
            Request       tmp           = requestList[requestList.Count - 1];
            PendingIntent pendingIntent = tmp.pendingIntent;

            requestList.Add(new Request(pendingIntent, tmp.requestCode, geofences));

            var task = geofenceService.CreateGeofenceListAsync(geofenceRequest.Build(), pendingIntent);

            try
            {
                await task;
                if (task.IsCompleted)
                {
                    log.Info(Tag, "Add GeoFence succeeded.");
                }
                else
                {
                    log.Error(Tag, $"Add GeoFence failed: {task.Exception.Message}");
                }
            }
            catch (System.Exception e)
            {
                log.Error(Tag, $"Add GeoFence exception: {e.Message}");
            }
            HMSGeofenceData.CreateNewList();
        }
Exemplo n.º 2
0
        public void CreateGeo()
        {
            Geofence fence = geoBuild
                             .setUniqueId("7")
                             .setRoundArea(LocationCallBackWrap.latitude, LocationCallBackWrap.longitude, 200)
                             .setConversions(7)
                             .setValidContinueTime(1000000)
                             .setDwellDelayTime(10000)
                             .setNotificationInterval(100)
                             .build();
            List geofenceList = new List();
            bool r            = geofenceList.add(fence.obj);

            GeofenceRequest.Builder builder = new GeofenceRequest.Builder();
            builder.createGeofenceList(geofenceList);
            builder.setInitConversions(7);
            GeofenceRequest request = builder.build();

            mService      = new GeofenceService(new Context());
            pendingIntent = getPendingIntent();
            mService.createGeofenceList(request, pendingIntent).addOnCompleteListener(new MCompleteListener());
        }
        public void AddGeofences(GeofenceModel geofenceModel)
        {
            //Set parameters
            geofenceModel.Id = Guid.NewGuid().ToString();
            if (geofenceModel.Conversion == 5) //Expiration value that indicates the geofence should never expire.
            {
                geofenceModel.Timeout = Geofence.GeofenceNeverExpire;
            }
            else
            {
                geofenceModel.Timeout = 10000;
            }

            List <IGeofence> geofenceList = new List <IGeofence>();

            //Geofence Service
            GeofenceService geofenceService  = LocationServices.GetGeofenceService(activity);
            PendingIntent   pendingIntent    = CreatePendingIntent();
            GeofenceBuilder somewhereBuilder = new GeofenceBuilder()
                                               .SetUniqueId(geofenceModel.Id)
                                               .SetValidContinueTime(geofenceModel.Timeout)
                                               .SetRoundArea(geofenceModel.LatLng.Latitude, geofenceModel.LatLng.Longitude, geofenceModel.Radius)
                                               .SetDwellDelayTime(10000)
                                               .SetConversions(geofenceModel.Conversion);;

            //Create geofence request
            geofenceList.Add(somewhereBuilder.Build());
            GeofenceRequest geofenceRequest = new GeofenceRequest.Builder()
                                              .CreateGeofenceList(geofenceList)
                                              .Build();

            //Register geofence
            //Task geoTask = geofenceService.CreateGeofenceList(geofenceRequest, pendingIntent);
            //geoTask.AddOnSuccessListener(new CreateGeoSuccessListener(activity));
            //geoTask.AddOnFailureListener(new CreateGeoFailListener(activity));
        }