iOS Question FCMDisconnect may block background notifications?

marcick

Well-Known Member
Licensed User
Longtime User
Hi everyone,
I've been using Firebase push notifications in my B4i app for many years. Overall, it has worked well, but I often receive reports from users saying that sometimes notifications stop arriving when the app is in the background or closed, even if it had just received some shortly before.


I followed the official tutorial which includes the following code:


B4X:
App.RegisterUserNotifications(True, True, True) 'allow badge, sound and alert
App.RegisterForRemoteNotifications    ' chiama Sub Application_PushToken
fm.Initialize("fm")

Sub Application_Background
   fm.FCMDisconnect
End Sub

Sub Application_Active
   fm.FCMConnect
End Sub

However, I suspect that calling fm.FCMDisconnect may actually prevent the app from receiving push notifications while it's in the background, or at least cause them to stop after a short time.
I ran tests using the official B4J sending tool: when the app goes to the background, it initially receives notifications, but after sometime, lets's say tenth of minutes, they completely stop arriving. This behavior matches what other users have reported searching in the forum.

At the moment, I'm trying to remove fm.FCMDisconnect and keep the connection always active to see if it improves reliability when the app is in the background.

My questions are:
  1. Is it possible that fm.FCMDisconnect breaks the connection to Firebase/APNs and stops notifications from being delivered while the app is not active?
  2. Why is this call still recommended in the tutorial? Is this approach still valid today?
  3. Has anyone solved similar issues by removing fm.FCMDisconnect?

Thanks to anyone willing to share their experience!

edit:
I'm noticing something interesting:
Application_Background is not called if the user explicitly closes the app (by swiping it away, for example), instead of just moving it to the background. So fm.FCMDisconnect is only called when the app is moved to the background — and in this case, notifications stop arriving shortly after.
On the other hand, if the user closes the app, fm.FCMDisconnect is not called, and notifications continue to arrive even after an hour.
This might explain why some users report that notifications sometimes stop arriving — they may not realize that it depends on how they “closed” the app.
So my question remains:
Why does the tutorial recommend calling fm.FCMDisconnect if this seems to actually prevent receiving notifications — especially considering that push notifications are most useful when the app is closed or inactive?
 
Last edited:

marcick

Well-Known Member
Licensed User
Longtime User
Yes, I'm using the B4J sending tool and my app has been working for several years, receiving notifications both when it's active and when it's in the background or closed.
However, from time to time, a user writes and complains: "I didn’t receive some notifications today."
There are other posts reporting this irregular behavior as well.
I've noticed that fm.FCMDisconnect is called only when the user puts the app in the background, and not when it's closed via swipe.
And it seems that once it's called, the app stops receiving notifications — either immediately or after some time.
On the other hand, if I never call it (I just tested this by leaving the phone with the screen off for a whole day without touching it, then sending a notification), it receives notifications just fine.


So now I’m wondering:
– Could this issue be caused by fm.FCMDisconnect?
– What exactly does this method do?
– Why should it be called?
– And why only when the app goes into the background, and not when it’s terminated?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I don't know — this is just the result of two specific tests I did between yesterday and today.
  1. With fm.FCMDisconnect left in Application_Background, if I put the app in the background and turn off the phone screen, it receives notifications, but if I don't touch the phone for 90 minutes, it stops. It completely stops receiving notifications.
  2. After removing fm.FCMDisconnect, the phone continues to receive notifications even after an entire day with the screen off and untouched.

It doesn't seem like a coincidence to me.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Yes, of course what you're saying is true, but I'm really going crazy here and I have to deal with clients telling me things like "I didn't receive any notifications today" every few days.
I repeated yesterday's tests, with and without fm.FCMDisconnect, and today—even after having the phone turned off for two hours—notifications are arriving regularly in both cases.

Should I just accept that the system is not 100% reliable and that some notifications might occasionally get lost?
To me, this is a serious issue.

Below is the code my server uses to send notifications. It seems to match the B4J sending tool.
When I check the logs, I always see that the notifications are sent successfully, even the ones that the client says were never received.

B4X:
    Dim ps As PushRecord
    ps=Main.KvsPushDb.Get(nr.DeviceID)
    Dim ex As Map
    ex.Initialize
    ex.Put("Firmware", nr.Firmware)
    ex.Put("Vid", nr.Vid)
    ex.Put("field1", "field1")
    ex.Put("Field2",100)
    Dim data As Map = CreateMap("title": nr.Title, "body": nr.Text, "extra": Mysub.maptostring(ex))

    If ps.DeviceType=Main.TYPE_IOS Then
        Dim message As Map = CreateMap("topic": nr.DeviceID, "data": data)
        Dim iosalert As Map=CreateMap("title": nr.Title, "body": nr.Text)
        ps.CurrentBadge=ps.CurrentBadge+1
        Main.KvsPushDb.Put(nr.DeviceID,ps)
        Dim Aps As Map=CreateMap("badge": ps.CurrentBadge)
        If nr.sound<>"NoSound" Then Aps.put("sound", nr.Sound)
        Dim PayLoad As Map=CreateMap("aps": Aps)
        Dim Apns As Map=CreateMap("headers": CreateMap("apns-priority": "10"), "payload": PayLoad)
        message.Put("notification", iosalert)
        message.Put("apns", Apns)
        Dim nn As String="IOS_" & shortid & ", Badge " & ps.CurrentBadge & ", sound: " & nr.sound & ", Title: " & nr.title & ", Msg: " & nr.Text   
    Else
        If nr.sound<>"NoSound" Then    data.put("sound", nr.Sound)
        Dim message As Map = CreateMap("topic": nr.DeviceID, "data": data)
        message.Put("android", CreateMap("priority": "high"))
        Dim nn As String="AND" & Mysub.Right(nr.DeviceID,5) & ", Badge " & ps.CurrentBadge & ", sound: " & nr.sound & ", Title: " & nr.title & ", Msg: " & nr.Text
    End If
    FirebaseSendMessage(message, nn)

End Sub

I don't know why I get an error adding this code section, I add an image of the remaining code

1754310663140.png
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Well, problem is that the user expect to be weaked up by a notification and once he opens the app he can see what's happening and the notification become unuseful .....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Erel, I can confirm you were right — and it couldn't have been otherwise — it was just a coincidence. Whether FCMDisconnect is called or not (eg whether the app is closed or moved to the background) doesn’t seem to make any difference.
The fact is that sometimes, after a certain period of inactivity (or after a certain period the app is not restarted), the app stops receiving notifications. I'm now testing by calling the following code both in Application_Start and Application_Active, so that for users who tend to never close the app and always leave it "running" (they believe) in the background, the registration for push notifications gets refreshed regularly. Hopefully, this will have a positive effect.

B4X:
Private Sub RegisterForPush
    Log("Registering for remote notification")
    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    fm.Initialize("fm")
    fm.FCMConnect
End Sub
 
Upvote 0
Top