I also found when running the code as per post 1, it will take 59926ms to 81882ms to run the function. The more messages I pass to function it will take longer and longer. It eventually takes 20-30 minutes before the message is received.
The code used to run ok, but all of a sudden it's running slow.
If I run this function like below (without the wait for) it will run that function within 130-200ms
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String, sound As String)
Dim n As Long = DateTime.now
Dim Token As String = GetTokenValue(ServiceAccountFilePath)
Dim Job As HttpJob
Job.Initialize("", Me)
Dim data As Map = CreateMap("title": Title, "body": Body)
Dim message As Map = CreateMap("topic": Topic, "data": data)
If Topic.StartsWith("ios_") Then
'B4i
Dim Badge As Int = 0
Dim iosalert As Map = CreateMap("title": Title, "body": Body)
message.Put("notification", iosalert)
message.Put("apns", CreateMap("headers": _
CreateMap("apns-priority": "10"), _
"payload": CreateMap("aps": CreateMap("sound": sound, "badge": Badge))))
Else
'B4A
message.Put("android", CreateMap("priority": "high"))
End If
Dim jg As JSONGenerator
jg.Initialize(CreateMap("message": message))
'Log(jg.ToPrettyString(4))
Job.PostString($"https://fcm.googleapis.com/v1/projects/${ProjectID}/messages:send"$, jg.ToString)
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "Bearer " & Token)
Log("Total Firebase Command Took: " & (DateTime.Now - n))
Job.Release
Return True
End Sub
However, even knowing it takes 130-200ms to run the above code, the push notification doesn't display on my phone for a few minutes. In some cases it can take 20 minutes. Most likely not the correct way in logging how long it took since it might have not been sent at that time and it just logged the value.
The Body of my push notification contains the timestamp it sent the message. This way I know when it hits my phone I know what time the server sent the push notification from my code. The timestamp matches the server time, but took time to arrive.
The way it triggers my SendMessage function is, I am sending a MQTT message to my B4J app. (the same MQTT message is sent to my B4A/B4i apps so I can see the MQTT message).
When the B4J app gets this MQTT message it checks a map to see if it contains the message already and if it does then it ignores the message and doesn't send the push notification. If it doesn't contain the message it will run the SendMessage function. (There is another MQTT message that will remove the message from the map.)
I have checked to see how long it took to check the map, and it takes 0-1ms. For troubleshooting I removed this part of the code, so I can rule this out in case it was somehow slowing down my code.
For troubleshooting, if I add 'Return True' to the start of the SendMessage function to stop the push notification from being sent then log all incoming MQTT messages it logs the message in realtime. Pretty much instant.
I also added an integer to the SendMessage function for troubleshooting and then got a Timer to log the value every 1 second. While it's not running the firebase code in the SendMessage function it was logging values between 10-20 every second. Therefore my code is running approx 10-20 messages per second.
If I then allow messages to be processed in the SendMessage function again, (as well as log the incoming MQTT message) the MQTT message arrives on my phone from the MQTT broker instantly (not the push notification but the MQTT message), and then I wait for the MQTT message to arrive in the B4J app and it took around 20-30 seconds before it logged the message in the IDE.
Seems that the SendMessage function is slowing down my B4J app completely. Before it was logging the MQTT instantly but while running the SendMessage firebase code it's not logging it instantly.
The integer value it logged every 1 second was now logging the values between 70-100. This is most likely queuing the messages since it's taking time to process the firebase code?
The above was done in debug mode. I then switch to release mode and got similar results where everything was delayed when it was running the firebase code.
I can't seem to work out why the code in the SendMessage function is running slow and not processing quickly and causing everything to run show.
I have tried it on different computers, and internet connections in case it was my internet connection, and all got the same result.