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

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

  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:
    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):
    Code:
    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 AND 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:
    Code:
    '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:
    Code:
    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:

    [​IMG]

    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: Jan 22, 2017
    Pendrush, derez, scsjc and 14 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 Well-Known Member 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 Well-Known Member 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 Well-Known Member 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:
     
  11. tufanv

    tufanv Well-Known Member 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:
    "application/json;charset=UTF-8"

    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.
    2016-06-10_11h34_30.png
    Screenshot_2016-06-10-11-40-13.png
     
    Last edited: Jun 10, 2016
    David Meier and DonManfred like this.
  16. Eme Fibonacci

    Eme Fibonacci Active Member Licensed User

    How unsubscribe?
    If
    Code:
    fm.Initialize("fm")
    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 Active Member Licensed User

    Code:
    fm.UnsubscribeFromTopic("topic-name")
     
    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).

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

     
    lomosami likes this.
  20. Eme Fibonacci

    Eme Fibonacci Active Member Licensed User

    You're right. Thank you.

    It's amazing how much B4J + Firebase + B4A can be powerful.
     
    Erel and AndOrNot like this.
Loading...