Android Question push message foreground vs background

paul fredrick

Member
Licensed User
Longtime User
why the push message with the app in background is correct and when is in foreground the message display null ?
thanks
 

paul fredrick

Member
Licensed User
Longtime User
this code sends the message correctly with some parameters in the 'data' section that are then handled by the app and saved on a sqllite table.
This happens correctly if the app is in the background, if in foreground the message is null.
Sending is done with B4J app (non-UI)
B4X:
Private Sub SendMessageToSingleDevice(Devtoken As String)

    Dim Job As HttpJob
    Job.Initialize("SendMessage", Me)

    Dim m As Map = CreateMap("to": $"${Devtoken}"$)
    Dim noti As Map = CreateMap("body":p2, "title":p3, "sound": "default")
    Dim data As Map = CreateMap("id":p4, "tr":p5, "tp":p6, "qt":p7, "sound": "default")
    m.Put("notification", noti)
    m.Put("data", data)
    m.Put("content_available": True)
    m.Put("priority", 2)
    m.Put("DefaultVibrate": True)

    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.Tag=jg
    Log(jg.ToString)
    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
 
Upvote 0

paul fredrick

Member
Licensed User
Longtime User
I did some tests and the 'null' message (in the status bar) occurs when the app is visible in the foreground, although there are data in the 'fm_MessageArrived' log.
In all other cases, the message arrives correctly.
This is the fm_MessageArrived code:
B4X:
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"), "")
    n.Notify(1)
    n.Sound=True
    n.Vibrate=True
End Sub

log:
Message data: {id=myp4, qt=myp7d, tp=myp6, tr=myp5, sound=default}

missing 'title' and 'body' but it also happens when the message arrives correctly
 
Last edited:
Upvote 0

paul fredrick

Member
Licensed User
Longtime User
B4X:
    Log($"Message title: ${Message.GetData.Get("title")}"$)
    Log($"Message body: ${Message.GetData.Get("body")}"$)
    Log($"Message data: ${Message.GetData}"$)

log:

Message title: null
Message body: null
Message data: {id=myp4, qt=myp7d, tp=myp6, tr=myp5, sound=default}

title and body is ever null (background or foreground) but in background i see title and body in message status bar
 
Last edited:
Upvote 0

paul fredrick

Member
Licensed User
Longtime User
thanks Erel for all,
what you say I did it first to take the first steps, now I have experienced many other things and I came to understand that the message sent with B4J app to trigger 'message_arrived' in all cases, you need to send only data .
In this way I get what interests me, the data always arrive, both with the application in the background, and in the foreground.
If the notification is active in B4J, the data only arrives with the application in the foreground.
So I ask you one thing:
in this mode (data only) the notification in the status bar does not give the possibility to open anything and remains persistent. (even if you try to intercept in 'activity_resume')

can I get data-only notification open something?
 
Upvote 0
Top