Android Question convert map to string

Isac

Active Member
Licensed User
Longtime User
hi, if I insert a string the message works, but if I leave (value) the message does not arrive

map = CreateMap("body1":value,"title1":value)

I thought maybe I need to convert from map to string
does anyone have an example?

Message arrived
Message data: ID 0:1532375920742306%959093b8f9fd7ecd, Data {body=GOOD DAY, title=HELLO}
Message=(RemoteMessage) com.google.firebase.messaging.RemoteMessage@be88JU
body1:
title1:
map=(MyMap) {body1=, title1=}


Code: Copy
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ID ${Message.MessageId}, Data ${Message.GetData}"$)
    Log("Message="& Message)
    Dim value As String
    Dim map As Map :map.Initialize   
    map = CreateMap("body1":value,"title1":value)
    For Each key As String In map.Keys
    value = map.Get(key)
    Log($"${key}:${value}"$)
Next
 
2. I don't see how it is related to Firebase push messages.

Code: Copy
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "kjkjjkjkjkt6DkjkjkjkjkjkjghjjhjhLkjkt"
End Sub

Sub AppStart (Args() As String)
    SendMessage("all", "This is the title", "Hello!!!!")
    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)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json")
    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
    StopMessageLoop '<-- non ui app only
End Sub


-----------------------------------------------------------------------------------



-----------------------------------------------------------------------------------
FirebaseMessaging


Code: Copy
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
    Dim mano As Bitmap
'    Dim value As String
End Sub




Sub Service_Create
    fm.Initialize("fm")

End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("all") '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: ID ${Message.MessageId}, Data ${Message.GetData}"$)
    Log("Message="& Message)
    Dim value As String
    Dim map As Map :map.Initialize
    map = CreateMap("body2":value,"title1":value)
    For Each key As String In map.Keys   
    value = map.Get(key)
    Log($"${key}:${value}"$)
    Next
    Dim jgen As JSONGenerator
    jgen.Initialize(map)
    Dim n As NB6
    mano = LoadBitmapResize(File.DirAssets, "mano.png", 24dip, 24dip, False)
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(mano)
    n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), jgen.ToString, Main).Notify(4)

End Sub
 
Upvote 0
Your question is not clear. Do you mean that if you leave nothing as body and value the message is not sent ?
Never tried, but why to do it ?
Can't you send at least a blank space ?
 
Upvote 0
hi marcick

Do you mean that if you leave nothing as body and value the message is not sent ?


When I send the message via b4j, the message is sent and received: see the log file
but it is not recovered from the value variable


Code: Copy
map = CreateMap("body2":value,"title1":value)

Message arrived
Message data: ID 0:1532421936812839%959458b8f9fd7ecd, Data {body=Hello!!!!, title=This is the title}
Message=(RemoteMessage) com.google.firebase.messaging.RemoteMessage@c99175a
body2:
title1:



if the message arrives, why is not taken from the variable (value)?

body2: in body2 I should find this ----> Hello!!!!

title1: in title1 I should find this ------>This is the title
 
Last edited:
Upvote 0
There are several mistakes in this code:
Code: Copy
Dim value As String
    Dim map As Map :map.Initialize
    map = CreateMap("body2":value,"title1":value)
    For Each key As String In map.Keys   
    value = map.Get(key)
    Log($"${key}:${value}"$)
    Next

As I already wrote to you, you should take the time and watch the collections video tutorial. You need to understand how to work with the Map collection.

1. You should never initialize a variable and then assign a new object to that variable.
2. 'value' will always be an empty string in the CreateMap line. You are not setting it.
3. I don't understand what the code above is supposed to do.
4. Instead of posting many lines of code, you should start with explaining what exactly you are trying to do.
 
Upvote 0
Fixed, that sweaty :)




Code: Copy
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ID ${Message.MessageId}, Data ${Message.GetData}"$)
    Log("Message="& Message)
    Dim map As Map :map.Initialize
    map = CreateMap("body2":Message.GetData.Get("title"),"title1":Message.GetData.Get("body"))
    For Each key As String In map.Keys 
    Dim value As String = map.Get(key)
    Log($"${key}:${value}"$)
    Next
    Dim jgen As JSONGenerator
    jgen.Initialize(map)
    Dim n As NB6
    mano = LoadBitmapResize(File.DirAssets, "mano.png", 24dip, 24dip, False)
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(mano)
    n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), jgen.ToString, Main).Notify(4)

End Sub
 
Last edited:
Upvote 0
this has not much sense:

Code: Copy
Dim map As Map :map.Initialize
map = CreateMap("body2":Message.GetData.Get("title"),"title1":Message.GetData.Get("body"))
For Each key As String In map.Keys
Dim value As String = map.Get(key)
Log($"${key}:${value}"$)
Next

and can be replaced with:

Code: Copy
log(Message.GetData.Get("title"))
log(Message.GetData.Get("body"))
 
Upvote 0
Back
Top