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

Status
Not open for further replies.

Erel

Administrator
Staff member
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.
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
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.

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

Last edited:

tufanv

Expert
Licensed User
this is a big addition after sending each user with smtp and gathering them in db files and splitting them when it is bigger than 1000 users .. GREAT !
 

valentino s

Active Member
Licensed User
Although it is not clear in their documentation you don't need any additional server.
Exactly my question. I'll find the answer. Please note "xmpp".

Implementation path
1 Set up the FCM SDK Set up Firebase and FCM on your app according the setup instructions for your platform.
2 Develop your client app Add message handling, topic subscription logic, or other optional features to your client app. During the development, you can easily send test messages from the Notifications console.
3 Develop your app server Decide which server protocol(s) you want to use to interact with FCM, and add logic to authenticate, build send requests, handle response, and so on. Note that if you want to use upstream messaging from your client applications, you must use XMPP.
and finally:
he server side of Firebase Cloud Messaging consists of two components:
  • FCM connection servers provided by Google. These servers take messages from an app server and send them to a client app running on a device. Google provides connection servers for HTTP and XMPP.
  • An app server that you must implement in your environment. This app server sends data to a client app via the chosen FCM connection server, using the appropriate XMPP or HTTP protocol.
A full FCM implementation requires both a client implementation and a server implementation. For more information about implementing the client side, see the client guide for your platform: iOS, Android, or Chrome.
 

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")
   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:



OMG! I cant believe how easy to send push . It took me 2 minutes.. I hope yuou can implement all firebase soluitoons to ios too . Great Work Erel !!
OMG! I cant believe how easy to send push . It took me 2 minutes.. I hope yuou can implement all firebase soluitoons to ios too . Great Work Erel !!
 
Last edited:

so27

Active Member
Licensed User
I've just noticed that messages with characters such as. , ö, ä, ü... as the value zero return. Is it correct that the MessageService not using special characters and punctuation marks is done? Or is it the SendMessage - processing?
 

so27

Active Member
Licensed User
Hi Erel,

how and where exactly do I the code ("application/json; charset = UTF-8") then install? Can you help me with there briefly?
 

Eme Fibonacci

Well-Known Member
Licensed User
How unsubscribe?
If
B4X:
 fm.Initialize("fm")
it is reinitialized previous subscribe will be lost ?
 
Last edited:

lomosami

Member
Licensed User
I remember GCM service was limit with a few message per day. Does this service have such limitation too?
 

DonManfred

Expert
Licensed User
Does this service have such limitation too?
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).

Announcement:
https://firebase.googleblog.com/2016/05/firebase-expands-to-become-unified-app-platform.html

Google Cloud Messaging, the most popular cloud-to-device push messaging service in the world, is integrating with Firebase and changing its name to Firebase Cloud Messaging (FCM). Available for free and for unlimited usage, FCM supports messaging on iOS, Android, and the Web, and is heavily optimized for reliability and battery-efficiency. It’s built for scale and already sends 170 billion messages per day to two billion devices.
 

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
 
Status
Not open for further replies.
Top