The wrapper encodes the message parameters using UTF-8 (by default ) as the destination is programmable by the server on the fly.
Here is the code used to encode the message
B4X:
Public Sub JobCreateParmList( Parameters() As String ) As String
Dim su As StringUtils
Dim sb As StringBuilder
Dim strRequest As String
sb.Initialize
For i = 0 To Parameters.Length - 1 Step 2
If i > 0 Then sb.Append("&")
If Parameters(i).Contains("=") = False And Parameters(i).Contains(">") = False And Parameters(i).Contains("<") = False Then
sb.Append(su.EncodeUrl(Parameters(i), "UTF8")).Append("=")
Else
sb.Append(su.EncodeUrl(Parameters(i), "UTF8"))
End If
sb.Append(su.EncodeUrl(Parameters(i + 1), "UTF8"))
Next
strRequest = sb.ToString
Return strRequest
End Sub
When the message is received (say using web socket or FCM), I am calling
B4X:
su.DecodeUrl(Message.Get("body"), "UTF8")
The above code, leaves the unwanted characters in the message
Here is the function used in the attached test code
B4X:
Private Sub SendTestFCMMessageToiOS( )
Dim Parameters() As String = Array As String ( "func", "hello", _
"Email", "someemailaddress@somedomain.com", _
"Msg", "This is a test") ', _
Dim Job1 As HttpJob
Job1.Initialize("Ignore", Me)
Dim MsgBody As String = Job1.JobCreateParmList( Parameters )
Dim FCMKey As String = "AIza....."
Dim Topic As String = "iOS_Topic."
HTTPW_SendFCMRequest( Topic, MsgBody, FCMKey)
End Sub
When decoded with B4A, the data looks exactly as in the Parameters array. When decoded with B4I: %40 & "+" stay in ( Sure, I can use replace later on, but I was hoping for a cleaner approach)
It still would be nice if StringUtils EncodeUrl/DecodeUrl would work consistently across all three platforms. Since it does not, one now as to keep that in mind when performing encoding in B4J and B4A, by running an additional step (replacing +'s with %20's) after using EncodeUrl.
To me, the code below should work the same on all three platforms (but does not):
B4X:
Dim su As StringUtils
Log(su.EncodeUrl("This is a test", "UTF8"))
Log(su.DecodeUrl(su.EncodeUrl("This is a test", "UTF8"), "UTF8"))
Log(su.DecodeUrl("This%20is%20a%20test", "UTF8"))
Log(su.DecodeUrl("This+is+a+test", "UTF8"))
B4A output:
This+is+a+test
This is a test
This is a test
This is a test
Tell you the truth, I've not tried any other characters. Doing some searches on this issue, it looks like the + is the sticking point (issue) when it comes to iOS handling encoding/decoding of URL information.
The B4J/B4A StringUtils' EncodeUrl/DecodeUrl are designed for "application/x-www-form-urlencoded" data. B4i's methods are more EncodeParametersOfAURL/DecodeParametersOfAURL functions then total URL encode/decode functions, since certain symbols are allowed before a ? of a URL then after the ? of a URL. Amazing how complicated something can get.
From what I can tell, you use JobCreateParamList to create a parameter list. From the code flow I can see, it does not need to be URL encoded (you're just decoding it in FCM_MessageArrived), so you could decode it before calling HTTPW_Send_FCMRequest(Topic, MsgBody, FCMKey) (and do away with the decoding in FCM_MessageArrived).
B4X:
Dim MsgBody As String = Job1.JobCreateParmList( Parameters )
Dim FCMKey As String = "AIza....."
Dim Topic As String = "iOS_Topic."
Dim su As StringUtils
MsgBody = su.DecodeUrl(MsgBody, "UTF8")
HTTPW_SendFCMRequest( Topic, MsgBody, FCMKey)
I'm not saying you did not find an issue (you did), but this way the decoding happens on the same platform as the encoding.
I am fully aware of what you are saying, and I already mentioned that.
If you look closely at the third post you can see what I am trying to achieve.
The application communication protocol is fully programmable by the server, and how messages are sent.
The same message can be sent via:
http
WebSocket
or directly using FCM
I know that I can put the necessary hooks to customize this, however I was trying to avoid that.