iOS Question Firebase Push Messages not receiving after a while

Mike1970

Well-Known Member
Licensed User
Hi, i'm trying to use Firebase to send Silent Push Notifications to my app.
The app, then, choose to show a notificaiton with LocalNotification or not.
The problem is that after a while the app is closed and no notification are coming for a while, when a new notification is sent it will never be received from the app (the event isn't raised)

B4X:
    #Entitlement: <key>aps-environment</key><string>production</string>
    'use the distribution certificate
    #CertificateFile: ios_distribution.cer
    'use the provision profile that goes with the explicit App Id
    #ProvisionFile: NewsItalia/NewsItalia.mobileprovision
    #PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>
B4X:
Private Sub Application_Start (Nav As NavigationController)
    Dim rl As ReleaseLogger
    rl.Initialize("192.168.0.8", 54323)
    
    analytics.Initialize
    NavControl = Nav
    NavControl.NavigationBarVisible = False
    SetNavColors(Colors.White)
    SplashScreenPg.Initialize("SplashScreenPg")
    FileManager1.Initialize
    SplashScreenPg.Title = "SplashScreen"
    SplashScreenPg.RootPanel.LoadLayout("SplashScreen")
    NavControl.ShowPage(SplashScreenPg)

    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    App.ApplicationIconBadgeNumber = 0

    firebaseMex.Initialize("firebaseMex")
End Sub
B4X:
Private Sub Application_Active
    firebaseMex.FCMConnect 'should be called from Application_Active
End Sub

Private Sub Application_Background
    firebaseMex.FCMDisconnect 'should be called from Application_Background
End Sub

Sub firebaseMex_FCMConnected
    firebaseMex.SubscribeToTopic("ios_notificationsTEST") 'add ios_ prefix to all topics
    firebaseMex.SubscribeToTopic("ios_articlesTEST")
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   
    Log($"Message arrived: ${Message}"$)
   
    'If Message.Get("topic") = "ios_notificationsTEST" Then
    '    Log("Notifications")
    '    MessageFromNotification(Message)
    'Else
    '    Log("Articles")
    '    MessageFromArticles(Message)
    'End If
   
    MostraNotifica(Message)
    CompletionHandler.Complete
End Sub

Sub MostraNotifica(Message As Map)
    Dim ln As Notification
    ln.Initialize(DateTime.Now)
    ln.IconBadgeNumber = BADGENUMBER+1
    ln.AlertBody = Message.Get("corpo")
    If App.OSVersion >= 8.2 Then
        Dim no As NativeObject = ln
        no.SetField("alertTitle", Message.Get("titolo"))
    End If
    ln.PlaySound = True
    ln.Register
    NOT_LAST = DateTime.Now
    FileManager1.AggiornaFile("Ultima_Notifica", NOT_LAST)
End Sub



What it could be?
 
Last edited:

Mike1970

Well-Known Member
Licensed User
How are you sending the messages?
I'm using the library pyFCM to use it in a python script (it works 100% of the time on android), i also tried to use a custom function made in python converting your B4J script (this one 👇).

Python:
def sendNotification(Key, Topic, Title, Body, OS, Silent):
    m = {"to":"/topics/"+Topic}
    data = {"title": Title, "body":Body}

    if OS == "iOS":
        if Silent == False:
            iosalert = {"title":Title, "body":Body, "sound":"default"}
            m.update({"notification":iosalert})
        else:
            m.update({"content_available": True})

    m.update({"priority": 10})
    m.update({"data":data})

    url = 'https://fcm.googleapis.com/fcm/send'

    headers = {'Content-type': 'application/json;charset=UTF-8', 'Authorization': 'key='+str(Key)}
    x = requests.post(url, data=str(json.dumps(m)), headers=headers)

I know i have to use the B4J tool, BUT i already tried and same result.
When i realized that the app wasn't receiving the notifications I tried with B4J and nothing.
 

Mike1970

Well-Known Member
Licensed User
We cannot help you if you don't follow the tutorial. Follow it carefully and it will work for you like it work for all others.
I already followed the tutorial to setup everything correctly, it was working.
But i discovered that in long-term using actually it doens't receive the messages even using the B4J tool.
When the app is open, everything works fine.

If I close the app , for 5/10 minutes i'm able to receive messages, after a while the event "remotemessage" doesn't fire anymore, until i open the app again.
 

Mike1970

Well-Known Member
Licensed User
I missed the fact that you are talking about silent push notifications. Silent push notification do work if implemented properly. However they are not very reliable.
Don't test it with iReleaseLogger as it can cause the app to crash when it starts in the background.
I actually began to use iReleaseLogger just to discover what was the problem :(

I tried to remove it again and try without.. the script is in a loop that send a notification every 5 minutes (just for testing).
After 4 notification it stopped receving
 

Mike1970

Well-Known Member
Licensed User
I missed the fact that you are talking about silent push notifications. Silent push notification do work if implemented properly. However they are not very reliable.
Don't test it with iReleaseLogger as it can cause the app to crash when it starts in the background.
It seems that, until its in background o foreground i works.
When is killed it start to not receiving notifications anymore
 

Semen Matusovskiy

Well-Known Member
Licensed User
This is expected. Silent pushes do not work, if app is not running (killed by user).
IMO, much easy to communicate with own web server and to receive commands from it in 'real' time (without delays).
 

Mike1970

Well-Known Member
Licensed User
This is expected. Silent pushes do not work, if app is not running (killed by user).
IMO, much easy to communicate with own web server and to receive commands from it in 'real' time (without delays).
Sorry for my ignorance, but I never done this things before.
How can I communicate with my server if the app is closed? what method I have to use ?
Thanks for answering 😃
 

Semen Matusovskiy

Well-Known Member
Licensed User
You can do nothing, when app is closed. But your web-server is able to keep a history of commands and your app is able to execute delayed commands, when user will activate an app.
 

Mike1970

Well-Known Member
Licensed User
P
You can do nothing, when app is closed. But your web-server is able to keep a history of commands and your app is able to execute delayed commands, when user will activate an app.
ho ok i understood something else before.
Oh ok. That isn’t the purpose unfortunately
Thanks for the tips, I will change the functionality 😃
 
Top