Hi,
Just checking if anyone else is noticing FCM is having a delay in delivering the push notification or is it just me ?
I have a few customers saying push notifications being sent from my server have a delay. I started to think maybe it's my server having a issue sending the push notification, but everything looked normal.
Yesterday (and for the past few months) it been fairly instant and delivered to my iPhone & Android devices within 1-2 seconds.
Today I sent a test push notification from my computer (to rule out my server) when I send a test push notification using my computer (using the code below, which is the same as what I am using on my server), Google's server accepted the API call but the push notification didn't arrive for at least 20-30 seconds later. I then run the API command again, but the push notification arrived after around 1 minute. I ran it again and it was delivered within around 10-15 seconds.
I am using the following code, and not sure if anyone else is also noticing a delay of the FCM being delivered to the phone after the API has been processed ?
The delay is happening for both Android and iOS, and I tried while my phone is connected to WiFi and cellular 5G connection, but had the same result.
Just checking if anyone else is noticing FCM is having a delay in delivering the push notification or is it just me ?
I have a few customers saying push notifications being sent from my server have a delay. I started to think maybe it's my server having a issue sending the push notification, but everything looked normal.
Yesterday (and for the past few months) it been fairly instant and delivered to my iPhone & Android devices within 1-2 seconds.
Today I sent a test push notification from my computer (to rule out my server) when I send a test push notification using my computer (using the code below, which is the same as what I am using on my server), Google's server accepted the API call but the push notification didn't arrive for at least 20-30 seconds later. I then run the API command again, but the push notification arrived after around 1 minute. I ran it again and it was delivered within around 10-15 seconds.
I am using the following code, and not sure if anyone else is also noticing a delay of the FCM being delivered to the phone after the API has been processed ?
The delay is happening for both Android and iOS, and I tried while my phone is connected to WiFi and cellular 5G connection, but had the same result.
B4X:
Sub Send
Wait For (SendMessage("ios_test", "title", "body", "sound")) Complete (Success As Boolean)
Log("Success = " & Success) ' this returns True
end Sub
Public Sub SendMessage(Topic As String, Title As String, Body As String, sound As String) As ResumableSub
Try
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))
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)
Job.GetRequest.Timeout = 10000
Wait For (Job) JobDone(Job As HttpJob)
If Job.Success Then
Log(Job.GetString) ' returns the successful message like { "name": "projects/... }
Job.Release
Else
Log(Job.ErrorMessage)
Log("Failed to process Firebase. Topic = " & Topic)
Job.Release
Sleep(10000)
SendMessage(Topic,Title,Body,sound)
End If
Job.Release
Return True
Catch
Job.Release
Log(LastException.Message)
Return False
End Try
End Sub
Private Sub GetTokenValue (FilePath As String) As String
Dim GoogleCredentials As JavaObject
GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(File.OpenInput(FilePath, ""))) _
.RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
Credentials.RunMethod("refreshIfExpired", Null)
Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub