Android Question [Resolved] FCM - Android to send the push messages

AymanA

Active Member
Licensed User
Hi All,

I have been following Erel's tutorial for how to implement FCM and I have successfully push the message using the B4J example.

I have searched the forum however I can not find a way to make the android itself publish the message, so my question, is this doable to make the android App send the FCM push notification?

If yes then can someone guide me to a link/code on how to implement this?

Thank you!
 

MarcoRome

Expert
Licensed User
Longtime User
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

it's the same thing ... here is the code:

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)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    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 btn_invia_Click

    'Se ho modificatola notifica faccio l'update
    If ft_notifica.Text.Length > 0   Then
        SendMessage("general", "NameApp", ft_notifica.Text)
    Else
        Msgbox("INSERIRE IL MESSAGGIO DA INVIARE","MESSAGGIO")
        ft_notifica.RequestFocusAndShowKeyboard
    End If
  
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
(old thread, I know, but... I think I have to write here).

Marco, you code is logical, of course, but... that way you have to write the API KEY on clients, which is a risk, expressly not recommended by Erel:
"Note that the API_KEY should be set in the B4J code. It shouldn't be distributed in your app".

Anyway, I have not found, on site, other solutions; do they exist?

(this question is addressed to anyone, of course).


P.S.

I suppose that the purpose of FCM is for a "source" to send messages to various clients (who must subscribe to a topic) without the clients being able to reply, send their own messages. Is this right?
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…