B4J Question FCM Delivery Delay

aaronk

Well-Known Member
Licensed User
Longtime User
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.


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
 

aaronk

Well-Known Member
Licensed User
Longtime User
For testing, I turned off my VPS cloud in case it was flooding firebase for some reason (which it isn't), and running the code from my computer and it is still delayed in being delivered.

Computer sends the API to Firebase.
Firebase returns successful JSON message.. Took 1311ms to complete.
iPhone gets the message around 15 seconds later.

re-ran the same test

Computer sends the API to Firebase.
Firebase returns successful JSON message.. Took 1281ms to complete.
iPhone gets the message around 43 seconds later.

The iPhone used to get the push notification within 1-2 seconds after it was successfully sent.

Is there new code to use to send the push notification ?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I created a new project in Firebase.

I used the new JSON files in my Android & iOS apps that I got from Firebase and now I am getting the push notification fairly instantly now.

I then used the files from my old Firebase project and it was running slow again.

Something must have changed in my original Firebase project and made the push notifications run slow all of a sudden. I will need to use the new firebase project files and submit an app update to address this issue until I work out what has changed in my original Firebase project.
 
Upvote 0
Top