B4A=true Group=Default Group ModulesStructureVersion=1 Type=Service Version=10.5 @EndOfDesignText@ Sub Process_Globals Private fm As FirebaseMessaging Dim db As SQL End Sub Sub Service_Create fm.Initialize("fm") db = Subs.dbInit End Sub Public Sub SubscribeToTopics fm.SubscribeToTopic("Trckr") 'Global (you can subscribe to more topics) fm.SubscribeToTopic("Sprvsr") fm.SubscribeToTopic("Sprv-ML") fm.SubscribeToTopic("Sprv-This") fm.SubscribeToTopic("Sprv-Cedex") fm.SubscribeToTopic("Sprv-GunaReparto") fm.SubscribeToTopic("Sprv-Durakelo") ' Log(fm.token) End Sub Sub Service_Start (StartingIntent As Intent) If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent) 'Sleep(0) 'Service.StopAutomaticForeground 'remove if not using B4A v8+. End Sub Sub fm_MessageArrived (Message As RemoteMessage) Log("Mensaje recibido") Log($"Message data: ${Message.GetData}"$) 'Si recibimos Pong, lo agregamos a la lista de dispositivos activos If Message.GetData.ContainsKey("t") And Message.GetData.Get("t") = "pong" Then Log("Recibimos pong "&Message.GetData.Get("d")) If Main.dispositivos.ContainsKey(Message.GetData.Get("d")) Then Dim dMap As Map = Main.dispositivos.Get(Message.GetData.Get("d")) Dim dispData As Map = dMap Else Dim dispData As Map = CreateMap("coords": "0,0", "d": Message.GetData.Get("d"),"v": Message.GetData.Get("v"), "w": Message.GetData.Get("w")) End If Log("** "&dispData) Main.dispositivos.Put(Message.GetData.Get("d"), dispData) End If 'Si el mensaje es de ubicacion recibida If Message.GetData.ContainsKey("t") And (Message.GetData.Get("t") = "u" Or Message.GetData.Get("t") = "au") Then Log("Recibimos ubicacion") Private ubi As Location Log("Llamamos UbicacionRecibida") If Message.GetData.Get("t") = "au" Then ' ToastMessageShow("Ubicacion recibida:"&Message.GetData.Get("body"),False) Private coords() As String = Regex.split(",", Message.GetData.Get("body")) ubi.Initialize ubi.Latitude = coords(0) ubi.Longitude = coords(1) Private c As Cursor = db.ExecQuery("select * from geocercas") 'Traemos las geocercas Private estaGC As Location If c.RowCount > 0 Then For i = 0 To c.RowCount - 1 c.Position = i estaGC.Initialize estaGC.Latitude = c.GetString("lat") estaGC.Longitude = c.GetString("lon") Log(ubi & "|" & estaGC) Log(ubi.DistanceTo(estaGC)) If ubi.DistanceTo(estaGC) < 300 Then 'Revisamos si la ubicaccion recibida esta dentro de alguna geocerca. ' ToastMessageShow($"Dentro de ${c.GetString("nombre")}"$, False) Subs.notiHigh("Dentro de Geocerca", $"${Message.GetData.Get("d")} esta dentro de ${c.GetString("nombre")}"$, 777, "Main") End If Next End If ' Subs.notiHigh("Dentro de Geocerca", $"${Message.GetData.Get("d")} esta dentro de ${c.GetString("nombre")}"$, 777, "Main") End If CallSub2(Main, "ubicacionRecibida", Message.GetData) End If 'Si el mensaje es de ruta gps recibida If Message.GetData.ContainsKey("t") And Message.GetData.Get("t") = "ruta" Then Log("Recibimos Ruta GPS") If Message.GetData.ContainsKey("r") Then Log("Tenemos Ruta") Main.base64=Message.GetData.Get("r") descomprimeRuta ' ToastMessageShow("Ruta Recibida: "&Message.GetData.Get("d"),False) Main.rRuta = Message.GetData.Get("d") CallSub(Main, "muestraRuta") End If End If CallSub(Main,"agregaAListview") End Sub Sub Service_Destroy End Sub Sub descomprimeRuta Dim su As StringUtils Dim decompressedbytes() As Byte = su.DecodeBase64(Main.base64) Log($"decompressedbytesLength: ${decompressedbytes.Length}"$) Dim bc As ByteConverter Main.rutaGPS = bc.StringFromBytes(decompressedbytes,"UTF8") Log($"uncompressedLength: ${Main.rutaGPS.Length}"$) ' Log($"Decompressed String = ${Main.rutaGPS}"$) End Sub