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

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jun 9, 2016.

Thread Status:
Not open for further replies.
  1. Erel

    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:

    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):
    Sub Process_Globals
    Private fm As FirebaseMessaging
    End Sub

    Sub Service_Create
    End Sub

    Public Sub SubscribeToTopics
    "general"'you can subscribe to more topics
    End Sub

    Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    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.Icon = 
    "title"), Message.GetData.Get("body"), Main)
    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:
    'Starter service
    Sub Process_Globals

    End Sub

    Sub Service_Create
    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.

    Attached Files:

    Last edited: Jul 15, 2019
    KZero, soyVB6, Hadi57 and 29 others like this.
  2. Shay

    Shay Well-Known Member Licensed User

    Thanks, are you going to provide B4i library soon, as well?
  3. Erel

    Erel Administrator Staff Member Licensed User

    Probably yes. However it will take some time.
    netkomm and valentino s like this.
  4. chefe82

    chefe82 Member Licensed User

    "This library requires B4A v6+"

    B4A V6+ When will this version
  5. DonManfred

    DonManfred Expert Licensed User

    beta will be released soon i guess
    chefe82 likes this.
  6. MarcoRome

    MarcoRome Expert Licensed User

    Yes confirmed By Erel ( Look HERE )
  7. DonManfred

    DonManfred Expert Licensed User

    I know. I already got the Mail ;-)
    MarcoRome and Erel like this.
  8. tufanv

    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 !
  9. MarcoRome

    MarcoRome Expert Licensed User

    I saw the email delayed :(:D:D
  10. valentino s

    valentino s Active Member Licensed User

    Exactly my question. I'll find the answer. Please note "xmpp".

    and finally:
    b4aexpress likes this.
  11. tufanv

    tufanv Expert Licensed 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: Jun 9, 2016
    fredo, valentino s and Daniel-White like this.
  12. so27

    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?
  13. Erel

    Erel Administrator Staff Member Licensed User

    Try to set the content type in the sender code to:

    If it will not work then you can always get the string bytes, encode them as base64 string with StringUtils and send the encoded string.
    hibrid0 and pesquera like this.
  14. so27

    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?
  15. so27

    so27 Active Member Licensed User

    Sorry I found it :(.

    Who wants to send messages with special characters, needs to add only the encoding of UTF8.
    Last edited: Jun 10, 2016
    David Meier and DonManfred like this.
  16. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    How unsubscribe?
    it is reinitialized previous subscribe will be lost ?
    Last edited: Jun 10, 2016
  17. lomosami

    lomosami Member Licensed User

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

    AndOrNot Well-Known Member Licensed User

    Eme Fibonacci likes this.
  19. DonManfred

    DonManfred 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).


    raad and lomosami like this.
  20. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    You're right. Thank you.

    It's amazing how much B4J + Firebase + B4A can be powerful.
    lurahxp, Erel and AndOrNot like this.
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice