mirror of
https://github.com/cheveguerra/FLP_2.0.git
synced 2026-04-17 11:26:12 +00:00
- Se puso que con cualquier notificacion revise si puede mandar la ubicacion. - Se puso que cuando se solicite la ubicacion con FirebaseMessaging, se mande tambien a PUSH_INFO.
200 lines
7.9 KiB
QBasic
200 lines
7.9 KiB
QBasic
B4A=true
|
|
Group=Default Group
|
|
ModulesStructureVersion=1
|
|
Type=Service
|
|
Version=10.5
|
|
@EndOfDesignText@
|
|
'///////////////////////////////////////////////////////////////////////////////////////
|
|
'/// Agregar estas lineas al editor de manifiestos
|
|
'
|
|
' CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
|
|
' CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
|
|
' CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
|
|
' CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
|
|
'
|
|
'/// Agregar modulo de servicio nuevo FirebaseMessaging y copiar este modulo
|
|
'
|
|
'/// Bajar el archivo google-services.json de la consola de Firebase (https://console.firebase.google.com/)
|
|
'/// El nombre de la app en el archivo json tiene que ser el mismo que el nombre del paquete (Proyecto/Conf de Compilacion/Paquete)
|
|
'
|
|
'/// En Starter agregar esta linea
|
|
'
|
|
' Sub Service_Create
|
|
' CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
|
|
' End Sub
|
|
'
|
|
'/// En Main en Sub Process_Globals agregar esta linea
|
|
'
|
|
' Private const API_KEY As String = "AAAAv__xxxxxxxxxxxxx-xxxxxxxxxxxxxx-xxxxxxxxxxxx"
|
|
'
|
|
'/// Esta llave se consigue igualmente en la consola de Firebase, configuracion de proyecto, Cloud Messaging,
|
|
'/// es la clave de servidor.
|
|
'///
|
|
'/// Se necesitan agregar las librerías: FirebaseAnalitics, FirebaseNotifications, JSON y OkHttpUtils
|
|
'/// ... JSON es necesario si se van a enviar mensajes, si solo se van a recibir, no es necesario.
|
|
'
|
|
'///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
Sub Process_Globals
|
|
Private fm As FirebaseMessaging
|
|
Private const API_KEY As String = "AAAAv1qt3Lk:APA91bECIR-pHn6ul53eYyoVlpPuOo85RO-0zcAgEXwE7vqw8DFSbBtCaCINiqWQAkBBZXxHtQMdpU6B-jHIqgFKVL196UgwHv0Gw6_IgmipfV_NiItjzlH9d2QNpGLp9y_JUKVjUEhP"
|
|
Dim locRequest As String
|
|
Dim pe As PhoneEvents
|
|
Dim batt As Int
|
|
Dim au As String
|
|
Dim Sprvsr As String = "Sprv-ML"
|
|
End Sub
|
|
|
|
Sub Service_Create
|
|
Subs.revisaBD
|
|
fm.Initialize("fm") 'Inicializamos FirebaseMessaging
|
|
Subs.getPhnId
|
|
pe.Initialize("pe") 'Para obtener la bateria
|
|
Subs.revisaBD
|
|
End Sub
|
|
|
|
Public Sub SubscribeToTopics
|
|
fm.SubscribeToTopic("Trckr") 'Global (you can subscribe to more topics)
|
|
fm.SubscribeToTopic("Trckr") 'Tracker Global
|
|
If Starter.logger Then Log("Subscrito a tracker global")
|
|
fm.SubscribeToTopic("Trckr-ML") 'Global (you can subscribe to more topics)
|
|
If Starter.logger Then Log("Subscrito a Trckr-ML")
|
|
fm.SubscribeToTopic(Starter.devModel) 'Propio (you can subscribe to more topics)
|
|
If Starter.logger Then Log("Subscrito a "&Starter.devModel)
|
|
If Starter.logger Then Log(fm.token)
|
|
fm.UnsubscribeFromTopic("Sprvsr") 'Unsubscribe from topic
|
|
' fm.UnsubscribeFromTopic("Trckr") 'Unsubscribe from topic
|
|
' fm.UnsubscribeFromTopic("Trckr-ML") 'Unsubscribe from topic
|
|
' fm.UnsubscribeFromTopic(Starter.devModel) 'Unsubscribe from topic
|
|
End Sub
|
|
|
|
Sub Service_Start (StartingIntent As Intent)
|
|
Subs.getPhnId
|
|
If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
|
|
Sleep(0)
|
|
Service.StopAutomaticForeground 'remove if not using B4A v8+.
|
|
StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, True) 'Iniciamos servicio cada XX minutos
|
|
End Sub
|
|
|
|
Sub fm_MessageArrived (Message As RemoteMessage)
|
|
If Starter.logger Then Log("Message arrived")
|
|
If Starter.logger Then Log($"Message from: ${Message.From}"$)
|
|
If Starter.logger Then Log($"Message data: ${Message.GetData}"$)
|
|
Subs.getPhnId
|
|
Subs.getSSID
|
|
If Message.GetData.ContainsKey("t") Then
|
|
Dim tipos As List = Regex.Split(",",Message.GetData.Get("t"))
|
|
If tipos.IndexOf("pu") <> -1 Or tipos.IndexOf("au") <> -1 Then 'Si es una peticion de ubicacion
|
|
If Starter.logger Then Log("Es una peticion de ubicacion")
|
|
If Starter.logger Then Log($"UUC: ${Starter.UUC.Latitude},${Starter.UUC.Longitude}"$)
|
|
If Starter.UUC.IsInitialized And Starter.UUC.Latitude <> 0.0 Then
|
|
' mandaLocFM(Starter.UUC)
|
|
Subs.actualizaVar("solicitudFM", 1)
|
|
If Starter.logger Then LogColor($"Llamamos mandaLoc2: ${Starter.UUC.Latitude},${Starter.UUC.Longitude},${Subs.formatoFecha(Starter.UUC.Time)}"$, Colors.Magenta)
|
|
Subs.mandaLoc2(Starter.UUC, Starter.devModel)
|
|
End If
|
|
locRequest="Activa"
|
|
If Starter.logger Then Log("Llamamos StartFLP")
|
|
CallSubDelayed(Tracker, "StartFLP")
|
|
End If
|
|
If tipos.IndexOf("au") <> -1 Then 'Si es una actualizacion de ubicacion
|
|
au = 1
|
|
End If
|
|
If tipos.IndexOf("ping") <> -1 Then 'Si es un ping
|
|
If Starter.logger Then Log("Es un ping")
|
|
If Starter.logger Then Log("Mandamos pong")
|
|
Dim params As Map = CreateMap("topic":Sprvsr,"title":"pong", "body":Starter.devModel&" - Recibi mensaje "&Message.GetData.Get("title"), "t":"pong")
|
|
SendMessage(params)
|
|
End If
|
|
If tipos.IndexOf("bgps") <> -1 Then 'Si es una instruccion de borrar archivo gps
|
|
If Starter.logger Then Log("Es una instruccion de borrar archivo gps")
|
|
If Starter.logger Then Log("Borramos archivo gps")
|
|
Subs.borramosArchivoGPS
|
|
End If
|
|
If tipos.IndexOf("bgps2") <> -1 Then 'Si es una instruccion de borrar db gps
|
|
If Starter.logger Then Log("Es una instruccion de borrar archivo gps")
|
|
If Starter.logger Then Log("Borramos archivo gps")
|
|
Subs.deleteGPS_DB
|
|
End If
|
|
If tipos.IndexOf("dr") <> -1 Then 'Si es una peticion de ruta gps
|
|
If Starter.logger Then Log("Es una peticion de Ruta GPS")
|
|
Dim rutaGpsCmp As String = Subs.dameRuta
|
|
Dim params As Map = CreateMap("topic":Sprvsr,"title":"ruta", "body":Starter.devModel&" - Recibi mensaje "&Message.GetData.Get("title"), "t":"ruta", "r":rutaGpsCmp, "fr": Main.fechaRuta)
|
|
SendMessage(params)
|
|
End If
|
|
If tipos.IndexOf("pu") = -1 And tipos.IndexOf("ping") = -1 And tipos.IndexOf("dr") = -1 Then
|
|
If Starter.logger Then Log("No es ping ni solicitud de ubicacion o ruta, entonces no hacemos nada")
|
|
End If
|
|
End If
|
|
' Dim n As Notification
|
|
' n.Initialize
|
|
' n.Icon = "icon"
|
|
' n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
|
|
' n.Notify(1)
|
|
End Sub
|
|
|
|
Sub Service_Destroy
|
|
|
|
End Sub
|
|
|
|
Sub SendMessage(params As Map)
|
|
Dim topic As String= params.Get("topic")
|
|
Dim title As String= params.Get("title")
|
|
Dim body As String= params.Get("body")
|
|
Dim tipo As String= params.Get("t")
|
|
If params.ContainsKey("r") Then
|
|
Dim rutaGpsCmp As String= params.Get("r")
|
|
Else
|
|
Dim rutaGpsCmp As String = ""
|
|
End If
|
|
Dim Job As HttpJob
|
|
Job.Initialize("fcm", Me)
|
|
Dim m As Map = CreateMap("to": $"/topics/${topic}"$)
|
|
Dim data As Map = CreateMap("title":title, "body":body, "d":Starter.devModel.Trim, "t":tipo, "w":Subs.ssid, "b":batt, "r":rutaGpsCmp, "v":Application.VersionName)
|
|
m.Put("data", data)
|
|
Dim jg As JSONGenerator
|
|
jg.Initialize(m)
|
|
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
|
|
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
|
|
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
|
|
If Starter.logger Then Log("Mandamos mensaje: " & m)
|
|
End Sub
|
|
|
|
Sub mandaLocFM(loc As Location)
|
|
Dim coords As String = loc.Latitude & ","&loc.Longitude & ","&Subs.formatoFecha(DateTime.Now)
|
|
If Starter.logger Then LogColor("Iniciamos mandaLocFM ("&coords&")", Colors.Magenta)
|
|
Dim t As String
|
|
If locRequest="Activa" Then
|
|
If au = 1 Then
|
|
t = "au" ' es una actualizacion
|
|
Else
|
|
t = "u" ' es una peticion
|
|
End If
|
|
Dim params As Map = CreateMap("topic":Sprvsr,"title":"ubicacionRecibida", "body":coords, "t":t)
|
|
' Log(params)
|
|
SendMessage(params)
|
|
locRequest="Enviada"
|
|
CallSubDelayed(Tracker,"CreateLocationRequest")
|
|
End If
|
|
End Sub
|
|
|
|
Sub pe_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
|
|
batt=Level
|
|
End Sub
|
|
|
|
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
|
|
If Starter.logger Then Log("Phone state: " & State)
|
|
If State = "RINGING" Then
|
|
If Starter.logger Then Log("The incoming number is: " & IncomingNumber)
|
|
Else
|
|
End If
|
|
End Sub
|
|
|
|
'Sub wifiScanned_ScanDone
|
|
' Log("//////////////////////////////wifi_conected_result")
|
|
' ToastMessageShow("Wifi_ConnectionResult",True)
|
|
' If Main.wifi.isWifiConnected Then
|
|
' ssid = Main.wifi.WifiSSID
|
|
' End If
|
|
'End Sub
|