Android Question FirebaseNotifications & Sender App

luke2012

Well-Known Member
Licensed User
Longtime User
I followed (some time ago) the @Erel related tutorial (FirebaseNotifications) and I have successfully implemented the FCM within my App.
I can send some test notifications using the Firebase console.

My question is:
Is there the possibility to implement an Android / Java App that allow a user to send notifications instead of using the firebase console ?

If I understood well I can implement the "sender" App starting from this code ?

B4X:
Private Sub SendMessage (Topic AsString, Title AsString, Body AsString)
Dim Job AsHttpJob
 Job.Initialize("fcm", Me)
Dim m AsMap = CreateMap("to": $"/topics/${Topic}"$)
Dim data AsMap = CreateMap("title": Title, "body": Body)
 m.Put("data", data)
Dim jg AsJSONGenerator
 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)
End Sub

Sub JobDone(job AsHttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
 job.Release
End Sub

Thanks in advance for your reply :)
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
You understood 100% and are 80% there !!

I am assuming that you have a "receiving" app AND a "sender" app and that you have done the whole registration thing on the Firebase console and have the API_KEY

You need to decide if you are sending to a subscribed or listed topic or a single user, and if it is a message (to be read by the user) or data (to be read by the device/app).

Here is a teaser of what I do:
I have a B4J back-end on which I update or add information to a MySQL database. This process then sends a "trigger" data message to my user base as a silent message.

In my app I have a service running that receives the message, triggers a sub that reads the latest information from my MySQL database and then updates the local SQLite database. All this without disturbing the user. Once this is done then I send a "local" message (without the FBM console) to my user to inform that there is new information available and if you really want to then you can take your receiving user straight to the information when they click on the message.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
If I understood well I can implement the "sender" App starting from this code ?

Yep. It's just that simple. To send a message just call the Google's API the way you posted. You can send a message from "everywhere" (e.g. PHP, etc.) just by calling the API.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
You understood 100% and are 80% there !!

I am assuming that you have a "receiving" app AND a "sender" app and that you have done the whole registration thing on the Firebase console and have the API_KEY

You need to decide if you are sending to a subscribed or listed topic or a single user, and if it is a message (to be read by the user) or data (to be read by the device/app).

Here is a teaser of what I do:
I have a B4J back-end on which I update or add information to a MySQL database. This process then sends a "trigger" data message to my user base as a silent message.

In my app I have a service running that receives the message, triggers a sub that reads the latest information from my MySQL database and then updates the local SQLite database. All this without disturbing the user. Once this is done then I send a "local" message (without the FBM console) to my user to inform that there is new information available and if you really want to then you can take your receiving user straight to the information when they click on the message.

Yes, in my scenario I have a "receiving" app AND a "sender" App (both Android apps).
My doubt are:

1) The "receiving" app AND a "sender" app must have the same package name? In my case I have two apps with differente package names.
2) The "sender" app could be also a java app (B4J UI app) ?
 
Upvote 0
Top