Android Question Firebase Notification only work from Firebase console

victormedranop

Well-Known Member
Licensed User
Longtime User
I have two simple project b4j and b4a.
in b4j a use this code to send message.

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region
Sub Process_Globals
    Private const API_KEY As String = "AIzaSySSSSSSSSSSSSqIa8sRdZzuQxo8"
End Sub
Sub AppStart (Args() As String)
    For x = 0 To 10
        SendMessage("all", "This is the title", "Hello!!!!") 
    Next
   
    StartMessageLoop
End Sub
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)
    Log( jg.ToString)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;chatset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    Sleep(1000)
End Sub
Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    StopMessageLoop '<-- non ui app only
End Sub

in my b4a i have the same function. when i used i received no error,
and return the message id.->{"message_id":5377313106300300542}

but i did not received nothing in my b4a applicacion.

but when i use the firebase console to send a message test works.

i already check my register to the topic. but no l0ke.

victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
Yes, the job is true and the result is the id of the message
i send this
B4X:
{"notification":{"body":"Notification ","title":"Hello!!!!"},"content_available":true,"data":{"data":"This is the title"},"to":"\/topics\/general"}
and received this
B4X:
{"message_id":5685970803103715176}


Victor
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
this is the code, thanks to DonManfred. i use the long server key and start working.

B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub
Sub Service_Create
    fm.Initialize("fm")
End Sub
Public Sub SubscribeToTopics
    If fm.IsInitialized Then 
        fm.SubscribeToTopic(public_data.topic) 'you can subscribe to more topics
        public_data.firebase_token = fm.Token
        LogColor(fm.Token,Colors.Red)
    Else 
        fm.Initialize("fm")
        fm.SubscribeToTopic(public_data.topic) 'you can subscribe to more topics
        public_data.firebase_token = fm.Token
        LogColor(fm.Token,Colors.Red)
    End If
    LogColor(fm.Token,Colors.Red)
End Sub
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    LogColor("Firebase Topic ->"& public_data.topic,Colors.Red)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Log(Message.GetData.Size)
    If Message.GetData.Size > 0 Then        
        Dim n As Notification
        n.Initialize
        n.Icon = "icon"
        n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
        n.Notify(1)
        CallSubDelayed3(chat_form,"received_data",Message.GetData.Get("title"),Message.GetData.Get("body"))
    Else
        CallSubDelayed3(chat_form,"received_data","Invalid message","Invalid Message")
    End If
End Sub
Sub Service_Destroy
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please note that you are using a SERVER Key in your B4A App which is not supposed to use one (app includes the KEY and can easily recompiled (Reveal the key)).

Security:
You should setup a B4J Server App which you call from B4A to send a Notification! And in this server app you should use the Serverkey.

You shouldn´t include a Serverkey in a B4A App placed on Playstore.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…