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.
You should also reference FirebaseAnalytics
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 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("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
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
See the code in the attached B4J tool. Note that the API_KEY should be set in the B4J code. It shouldn't be distributed in your app.
API_KEY is the server key from:

A simple non-ui B4J program is attached.
Note that messages sent from Firebase Console will not arrive in some cases. Use the B4J code to test it.
Attachments
-
1.1 KB Views: 1,385
Last edited: