Android Question (SOLVED)FIREBASE (B4A)

RAFA BRAVO

Active Member
Licensed User
Longtime User
Hello good afternoon. I have a question.
I send from B4R a request to notification phus to FIREBASE, through a server in B4J, it works well,

but I can't hear the sound of the notification, nor can I see it with the phone blocked.
it is a code or configuration problem in FIREBASE
CODE B4A:
B4A:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

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 Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    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.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

CODE B4J:
B4J Server:
Sub Process_Globals
    Private const API_KEY As String = ""
    Private server As ServerSocket
    Public connected As Boolean
    Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
    server.Initialize(51042, "server")
    server.Listen
   
   
    StartMessageLoop
End Sub

Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("new connection")
    astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "Astream")
    server.Listen
End Sub

Sub Astream_NewData (Buffer() As Byte)
    If Buffer(0) = 0 Then
        Log("Button is down.")
    Else
        Log("Button is up.")
        SendMessage("general", "ATENCION", "paro de linea")
    End If
    astream.Write("Thank you for this useful information.".GetBytes("UTF8"))
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)
    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 JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
'   ExitApplication '!
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a mistake to post your API key in the forum.
The B4J code is not relevant.
The first snippet is B4A, not B4R code.

The behavior depends on your phone settings.
Make sure that the phone is not muted.
Initialize the notification with n.Initialize2 and set the importance level to HIGH.
Add n.Sound = True.
 
Upvote 0
Top