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

Status
Not open for further replies.
Updated tutorial: https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/


Clarification: The nice thing about FCM is that your app doesn't need to run in order to receive messages. The FirebaseMessaging receiver will be started by the OS when a new message arrives.
It is not a real issue, but if the user closed your app with a "force close" from the device settings then messages will not arrive, until the app is started again.
On some misbehaving devices, killing the app with a swipe will also cause messages not to arrive, until the app is started again.

Firebase Cloud Messaging service is a layer above Google Cloud Messaging service.
It makes it simple to add support for push messages.

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 Receiver named FirebaseMessaging to your app (must be this name):
B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general")
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_DEFAULT)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)  
End Sub

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
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

  • B4J-SendingTool.zip
    1.1 KB · Views: 928
Last edited:

tufanv

Expert
Licensed User
Longtime 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
Longtime 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".


and finally:
 

tufanv

Expert
Licensed User
Longtime User

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
Longtime 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
Longtime 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?
 

so27

Active Member
Licensed User
Longtime User
Sorry I found it .

Who wants to send messages with special characters, needs to add only the encoding of UTF8.

 
Last edited:

Eme Fibonacci

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

lomosami

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

DonManfred

Expert
Licensed User
Longtime 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

 

Duncan H Williamson

Member
Licensed User
Longtime 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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…