iOS Question Push notification only works when app is in foreground

biometrics

Active Member
Licensed User
Longtime User
Push notifications (using Firebase) is only working for me when the app is in the foreground. Application_PushToken fires as expected. When I go to the home screen and send a notification to my app nothing happens, there is no indication from the OS that there is a notification. When I then open the app the notifications come though again.

I've tried Debug and Release mode.

I've got this in #Region:

B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>

I've got this in Application_Start:

B4X:
App.RegisterUserNotifications(True, True, True)
App.RegisterForRemoteNotifications

In Android I had to do this in fm_MessageArrived to create a notification on the status bar:

B4X:
n.Initialize
n.Icon = "icon"
n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("message"), Main)
n.Notify(1)

But it seems on iOS the OS does it automatically if I read the threads correctly. Or am I wrong?

I tried adding this to Application_RemoteNotification:

B4X:
Dim n As Notification

n.Initialize(DateTime.Now)
n.AlertBody = Message.Get("title") & " - " & Message.Get("message")
n.PlaySound = True
n.Register

But that only seems to fire Application_ReceiveLocalNotification when the app is in the foreground and there is nothing on the OS status bar.

What am I missing?
 

biometrics

Active Member
Licensed User
Longtime User

Thanks that helped. One of the later comments said you needed to add:

B4X:
"notification":{"click_action":".MainActivity","body":"new Symulti update !","title":"new Symulti update !","icon":"ic_notif_symulti"},

And that made it work on iOS. It stopped working on Android though. So I now build an array of the Android devices and sent it without and then I build an array of iOS devices and send it with. So now it works on both.
 
Last edited:
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
The code from the tutorial works in all modes: https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/

You should make sure that iOS devices are registered to topics that start with ios_

Notifications are sent from my server to Firebase with PHP code. It builds a JSON string. It was working fine on Android, on iOS it only worked when the app was in the foreground. The link above helped me figure out iOS also needed "notification" in the JSON. It's all working nicely now. Only thing missing is the app icon badge number on iOS, I started a new thread.
 
Upvote 0

b4auser1

Well-Known Member
Licensed User
Longtime User
The code from the tutorial works in all modes: https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/

You should make sure that iOS devices are registered to topics that start with ios_
In fact the code of SendTool just bulds different json strings for ios and android. It also adds "notification" node for ios devices.

B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…