Android Question Firebase notification (from VBA): solved

dibesw

Active Member
Licensed User
Longtime User
If it can serve someone, here is an example for send Firebase Notification from VBA code
that it took me a while to make it work.

B4X:
Private Sub Form_Load()
notifica_firebase "Studio dentistico TecnoDent", "Ciao, ti ricordo l'appuntamento di domani alle ore 10:30", "https://api.androidhive.info/images/minion.jpg"
End Sub

B4X:
Public Sub notifica_firebase(TITOLO As String, TESTONOTIF As String, IMAGEURL As String)
    On Error GoTo NOINTERNET
    Dim sURL As String
    Dim PostData As String
    sURL = "https://fcm.googleapis.com/fcm/send"
    Dim objXMLHTTP As MSXML2.ServerXMLHTTP
    Set objXMLHTTP = New MSXML2.ServerXMLHTTP
    PostData = "{""data"":{""title"":""" & _
    TITOLO & _
    """,""body"":""" & _
    TESTONOTIF & _
    """,""image"":""" & _
    IMAGEURL & _
    """},""to"":""ezCu68iMQKuDJK0yY5Km9s:APA91bHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCnaBKn9at-ai9_70vyaEYUlF4RD3YFNipTPyhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrNvuGouXrnRN0NXhxAHEXr""}"""
'ezCu68iMQKuDJK0.............is mobile TOKEN
    With objXMLHTTP
       .Open "POST", sURL, False
       .SetRequestHeader "Content-Type", "application/json"
       .SetRequestHeader "Authorization", "Bearer AAAAiaPkBPoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxiCm1y11tG49WN1SaO-XWk2baT271JKDgxxxxxxxxxxxxxxxxxxxxxxxxxxYED5p7pqnAbJ2"
       .Send (PostData)
    End With
'Bearer AAAAiaPkBPoxxxxxx..................is server key
    Response = objXMLHTTP.ResponseText
         freeappo = FreeFile
         Open "c:\infostudio\notifica.txt" For Output As #freeappo
         Write #freeappo, objXMLHTTP.ResponseText
         Close #freeappo
    Exit Sub
NOINTERNET:
    MsgBox "CONTROLLARE LA CONNESSIONE AD INTERNET " & err.Description, vbCritical
End Sub

Screenshot (16).png
 

aeric

Expert
Licensed User
Longtime User
Sending from MS Excel ?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Great job!
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
If it can serve someone, here is an example for send Firebase Notification from VBA code
that it took me a while to make it work.

B4X:
Private Sub Form_Load()
notifica_firebase "Studio dentistico TecnoDent", "Ciao, ti ricordo l'appuntamento di domani alle ore 10:30", "https://api.androidhive.info/images/minion.jpg"
End Sub

B4X:
Public Sub notifica_firebase(TITOLO As String, TESTONOTIF As String, IMAGEURL As String)
    On Error GoTo NOINTERNET
    Dim sURL As String
    Dim PostData As String
    sURL = "https://fcm.googleapis.com/fcm/send"
    Dim objXMLHTTP As MSXML2.ServerXMLHTTP
    Set objXMLHTTP = New MSXML2.ServerXMLHTTP
    PostData = "{""data"":{""title"":""" & _
    TITOLO & _
    """,""body"":""" & _
    TESTONOTIF & _
    """,""image"":""" & _
    IMAGEURL & _
    """},""to"":""ezCu68iMQKuDJK0yY5Km9s:APA91bHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCnaBKn9at-ai9_70vyaEYUlF4RD3YFNipTPyhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrNvuGouXrnRN0NXhxAHEXr""}"""
'ezCu68iMQKuDJK0.............is mobile TOKEN
    With objXMLHTTP
       .Open "POST", sURL, False
       .SetRequestHeader "Content-Type", "application/json"
       .SetRequestHeader "Authorization", "Bearer AAAAiaPkBPoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxiCm1y11tG49WN1SaO-XWk2baT271JKDgxxxxxxxxxxxxxxxxxxxxxxxxxxYED5p7pqnAbJ2"
       .Send (PostData)
    End With
'Bearer AAAAiaPkBPoxxxxxx..................is server key
    Response = objXMLHTTP.ResponseText
         freeappo = FreeFile
         Open "c:\infostudio\notifica.txt" For Output As #freeappo
         Write #freeappo, objXMLHTTP.ResponseText
         Close #freeappo
    Exit Sub
NOINTERNET:
    MsgBox "CONTROLLARE LA CONNESSIONE AD INTERNET " & err.Description, vbCritical
End Sub

View attachment 112925
Nice job..How do you get the mobile token?
 
Upvote 0

dibesw

Active Member
Licensed User
Longtime User
Nice job..How do you get the mobile token?
Can you get mobile token from your app in Activity with StartService:
B4X:
StartService(FirebaseMessaging)
that is the service that I used...
B4X:
#Region Module Attributes
    #StartAtBoot: true
#End Region
Sub Process_Globals
   Private fm As FirebaseMessaging
   Dim n As Notification
   'Dim n2 As NotificationBuilder
End Sub

Sub Service_Create
   fm.Initialize("fm")
    ''fm.SubscribeToTopic("general") 'you can subscribe to more topics
   Do While fm.Token = Null Or fm.Token = ""
        Wait(1)
   Loop
   Main.Tokken=fm.Token
  n.Initialize
   'n2.Initialize
   ''Service.StartForeground(1,n)
End Sub
Public Sub SubscribeToTopics
    'ToastMessageShow("PPP",True)
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub
Sub Service_Start (StartingIntent As Intent)
   If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
    'Service.StopAutomaticForeground
End Sub
Sub Service_Destroy
End Sub
Sub Wait(Sekunden As Int)
   Dim Ti As Long
   Ti = DateTime.Now + (Sekunden * 1000)
   Do While DateTime.Now < Ti
      'DoEvents
   Loop
End Sub

Main.Tokken is mobile Token (that you should record somewhere)
 
Upvote 0

amorosik

Well-Known Member
Licensed User
If it can serve someone, here is an example for send Firebase Notification from VBA code
that it took me a while to make it work.

Excellent example, very useful for me
How to do the opposite, that is a routine that 'hears' the reception of a message from Firebase to Vba?
The ideal would be a system that allows you to create a new event in the Vba code, but even the simple start of a function from the outside would be sufficient
 
Upvote 0

dibesw

Active Member
Licensed User
Longtime User
To do this you should look Remore Database Connector.
It's not very easy to understand;
the best situation is to write to a DB (via app) and read this DB from VBA (which is listening). When the app raises an event on the DB then VBA "hears" it and raises an event
 
Upvote 0

amorosik

Well-Known Member
Licensed User
To do this you should look Remore Database Connector.
It's not very easy to understand;
the best situation is to write to a DB (via app) and read this DB from VBA (which is listening). When the app raises an event on the DB then VBA "hears" it and raises an event

Yes, data polling is a system that works
However, it does not allow to have a reactive system as one would expect
This is because the db can be interrogated at intervals of time, and it is not possible to do it very quickly because otherwise the system is unnecessarily burdened.
Suppose we perform a db reading every 5 seconds, this means that in the worst case an event that reaches the db must wait 5 seconds to be served
Let's say it's okay to start, but if there is a better system it would be preferable
 
Upvote 0

dibesw

Active Member
Licensed User
Longtime User
Yes, data polling is a system that works
However, it does not allow to have a reactive system as one would expect
This is because the db can be interrogated at intervals of time, and it is not possible to do it very quickly because otherwise the system is unnecessarily burdened.
Suppose we perform a db reading every 5 seconds, this means that in the worst case an event that reaches the db must wait 5 seconds to be served
Let's say it's okay to start, but if there is a better system it would be preferable
Yes, unfortunately it is: the solution I found works with a set timer on 30sec that polls a sql table; when this table receives data then an event is triggered;
i couldn't think of anything else
 
Upvote 0

amorosik

Well-Known Member
Licensed User
Yes, unfortunately it is: the solution I found works with a set timer on 30sec that polls a sql table; when this table receives data then an event is triggered;
i couldn't think of anything else

30 seconds is a very high time
The responsiveness of the system as a whole, I believe, cannot be satisfactory in most applications
Here a similar thing was requested, getting a response with information that is currently beyond my ability.
I would ask you to take a look, maybe together we might be able to get a better functionality than simple polling
 
Upvote 0

dibesw

Active Member
Licensed User
Longtime User
30 seconds is a very high time
The responsiveness of the system as a whole, I believe, cannot be satisfactory in most applications
Here a similar thing was requested, getting a response with information that is currently beyond my ability.
I would ask you to take a look, maybe together we might be able to get a better functionality than simple polling
I don't need an immediate answer then I use 30sec for my timer, but no one forbids you to put 1 sec or 100msec.
I think if you use a timer like that you can cope with any need
 
Upvote 0

amorosik

Well-Known Member
Licensed User
I don't need an immediate answer then I use 30sec for my timer, but no one forbids you to put 1 sec or 100msec.
I think if you use a timer like that you can cope with any need

"..but no one forbids you .."
In reality there is who / what prevents you from increasing the interrogation frequency
It is the occupation of resources dedicated to the questioning
Yes, it is possible to interrogate even at 100 mSec
But if the database takes 80 mSec to perform the query, then practically all the resources of the pc are allocated to do just that thing
Even if only one event occurs per day, 864,000 queries will be performed
Not exactly the ideal way to proceed
It works, yes, but if it could be improved, it would be better
 
Upvote 0

dibesw

Active Member
Licensed User
Longtime User
"..but no one forbids you .."
In reality there is who / what prevents you from increasing the interrogation frequency
It is the occupation of resources dedicated to the questioning
Yes, it is possible to interrogate even at 100 mSec
But if the database takes 80 mSec to perform the query, then practically all the resources of the pc are allocated to do just that thing
Even if only one event occurs per day, 864,000 queries will be performed
Not exactly the ideal way to proceed
It works, yes, but if it could be improved, it would be better
you're absolutely right, but that's all I can think of right now. There would be a little study but I'm dedicated to other things
 
Upvote 0
Top