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

aaronk

Well-Known Member
Licensed User
Longtime User
My FCM is slow again after a few weeks. It started to run slow a week or two ago.

The message arrived in less than a second.
Try creating a project that sends 2-3 messages every 1-2 seconds and have it running for a few weeks.
Then in a few weeks time, make it so it sends a FCM to your phone and see how long it takes to get delivered to the phone after the API is successful.
Then keep the Firebase project still sending the messages for another few weeks, and then see how long the FCM arrives.

I created a new project and it was fairly instant. Then after 1-2 weeks it took around 5-7 seconds to arrive, then after 2-3 weeks it started to get slower in being delivered. Currently it's taking around 10-15 seconds for the FCM message to be delivered after I send the API to Firebase. Sometimes even 20-25 seconds.

It would be interesting to see if you get the same result as me when having it running for a few weeks.

Someone else also reported it being slow for them as well, so it's either Firebase delaying the message in there server, or we are missing something in the API call. I used the example SendingTool B4J Project when sending a test message to my phone for testing.

For some reason other apps are OK like Gmail, bank app (bank telling me I made a payment etc), but my B4A/B4i app has the message delay.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Do you report this issue to firebase team?
Yes, but they couldn't help as AI robots were emailing me as they kept using 'Templates' as the reply.

I originally emailed them at the start of the year as I was having an issue. I found my DNS on my server had an issue. Now I am having an issue with the delay of the delivery of the notification. I got the same email as I emailed them in the past.

However, it's delayed at the moment but it doesn't mention any outages at the moment on there status page. That is the first page I looked at when I originally got the delays.
 
Upvote 0
Top