B4A Library FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)

Status
Not open for further replies.

Duncan H Williamson

Member
Licensed User
Hi
Fantastic to be able to use firebase, I have followed the instructions above and in the intergrating firebase services to get this running, however I cannot find where to get the API_KEY for the server example.... yet another grey moment on my part I guess.... I have registered my project and downloaded the relevent file for the B4A client but sofar cannot find the key :( could someone please point me in the right direction

Regards

Duncan
 

tufanv

Expert
Licensed User
This library requires B4A v6+.

Firebase Cloud Messaging service is a layer above Google Cloud Messaging service.
It makes it simple to add support for push messages. Although it is not clear in their documentation you don't need any additional server.

Sending messages is done with a simple HTTP request. It is also possible to send message from the Firebase console, though it is not very useful and is actually more complicated than using the REST api.

1. The first step is to follow the Firebase integration tutorial:
https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

Make sure to add the Notifications snippet.

2. Add a Service named FirebaseMessaging to your app (must be this name):
B4X:
Sub Process_Globals
   Private fm As FirebaseMessaging
End Sub

Sub Service_Create
   fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
   If fm.HandleIntent(StartingIntent) Then Return
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   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
fm_MessageArrived will be raised whenever a message is received. In this case we show a notification. You can do whatever you need.

We call SubscribeToTopics from the starter service to make sure that the app will be subscribed when it starts:
B4X:
'Starter service
Sub Process_Globals

End Sub

Sub Service_Create
   CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub
Now we can send messages to a "topic" and all the subscribed devices will receive it.

This is done with this request:
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
   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)
   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)
End Sub


Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
End Sub
API_KEY is the server key from:



A simple non-ui B4J program is attached.
We can send the notification dişrectly from firebase console. Why did Erel said that it is complicated ? does it have some weaknesses or what ?
 

aidymp

Active Member
Licensed User
When I send a message from the firebase console it get null, null but from the b4j app it works fine? why is that?

Thanks

Aidy
 

tiziana.tinnirello

Member
Licensed User
How to subscribe more topics ?

executing code above has no effect.

fm.SubscribeToTopic("general") 'you can subscribe to more topics
fm.SubscribeToTopic("another") 'you can subscribe to more topics
 

Erel

Administrator
Staff member
Licensed User
We can send the notification dişrectly from firebase console. Why did Erel said that it is complicated ?
It is simple to send from Firebase console however there are several edge cases that you need to handle (go over their documentation and you will see) in order to always receive the messages.
I don't find it useful as it is very simple to send messages with the REST API.

How to subscribe more topics ?
executing code above has no effect.
Your code is correct. It should work.
 

aidymp

Active Member
Licensed User
How to subscribe more topics ?

executing code above has no effect.

fm.SubscribeToTopic("general") 'you can subscribe to more topics
fm.SubscribeToTopic("another") 'you can subscribe to more topics
That does work for me! one point i noticed, is if your using an emulator it must have google play services installed or all the code has no effect, although adsense still works! (This may be pretty obvious but took me an hour to realise :)
 

Erel

Administrator
Staff member
Licensed User
Probably yes. I cannot yet say when will it be available.
 

dragonguy

Active Member
Licensed User
What the different about gcm and fcm? I have many app implement with gcm, is it worth change to fcm?
 

Leni Berry

Active Member
Licensed User
i hv an error around this code below :

Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)

Note:
i already use httputil2 library by B4A v6
 

Attachments

Last edited:

KMatle

Expert
Licensed User
As far as i know no. Or, let me say, i did not found any info about restrictions.

Only the info that it is totally free (and forever).
About a month I tested one of my Apps using GCM. I must have sent thousands of messages in 8 hours or so :cool: As long as you don't spam Google's servers, everything is ok...

Even if you don't need a server here I strictly recoomend to have some logic between it (like login, check who sends messages, handling devices etc.). If someone catches your api-key (stored inside the app) then you're done :). So send it to your server, check it, route it to the device (with the corresponding php).
 
Status
Not open for further replies.
Top