iOS Question B4i - Firebase Push Notification - Clarification

Darren69

Member
Licensed User
Longtime User
Hi All,

I have finally gotten around to updating my B4A and B4i applications and wanted to check something before I started to dig in to why something is not working the way I expected it to.

With the Android version regardless of if the application is running sending a firebase notification is shown in the taskbar, or in the notifications, all good. On the iOS version, if the application is not running nothing shows up, I think I have read in other places that this is normal, but if it is, then this may not be the option for me...

So can someone please confirm if that is the cae,
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
On the iOS version, if the application is not running nothing shows up, I think I have read in other places that this is normal
No. If the application is in the foreground then RemoteNotification event is raised. If the app is in the background or not running then a notification is shown.

Make sure to accurately follow the tutorial and it will work.
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
Thanks as always Erel, I will dig to see what I missed then, because I currently only get notifications when the application is in the foreground.
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
Hi everyone

I had to put this down for a little while, but have looked at it again.

I am still not getting push notifications to work when the app is in the background, re followed the tutorial, copied and pasted the code from the tutorial as was, changed to the correct mobile provision name, and the same thing, I can receive push notifications when the application is in the foreground, but when its not running or the application is in the background I do not get a notification until the application is back in the foreground.

I followed the tutorial, and don't think I skipped a step, the 'only' thing I have not tried is create a new standalone firebase application, I just added the iOS test version to the existing android test one I made when i was getting the android one working..

Could apple have changed something? because clearly the tutorial shows it working..

does anyone have any thoughts on this because I am really scratching my head here trying to figure this out.

thanks

Darren
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
I have tried this again 3 more times, from scratch each time, I have also tried creating a brand new firebase project in the console, and updated my iOS test phone to the latest 11.4

I have tried on Wifi, and with a sim card in.

I still get the same thing - if the application is in the foreground, i get the message box with the push notification, if the application is in the background, or not running I do not get anything until I next open the application.

Also just for a laugh I tried it on an ipod touch, same thing. :(

I am pretty sure I am not missing a step, so could someone please humor me, and try following the tutorial from scratch again and see if they get the same result, because I am now at that brick wall that I can't overcome.

here is the code I copied and pasted, and then changed my provision profile to the one I am using.

B4X:
#Entitlement: <key>aps-environment</key><string>production</string>
'use the distribution certificate
#CertificateFile: ios_distribution.cer
#ApplicationLabel: Push Test again
'use the provision profile that goes with the explicit App Id
#ProvisionFile: pushtest.mobileprovision
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private analytics As FirebaseAnalytics
    Private fm As FirebaseMessaging
End Sub

Private Sub Application_Start (Nav As NavigationController)
    analytics.Initialize
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    fm.Initialize("fm")
End Sub

Private Sub fm_FCMConnected
    Log("FCMConnected")
    'here we can subscribe and unsubscribe from topics
    fm.SubscribeToTopic("ios_general") 'add ios_ prefix to all topics
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    Log($"Message arrived: ${Message}"$)
    Msgbox(Message, "Push message!")
    CompletionHandler.Complete
End Sub

Private Sub Application_Active
    fm.FCMConnect 'should be called from Application_Active
End Sub

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

Sub Application_PushToken (Success As Boolean, Token() As Byte)
    Log($"PushToken: ${Success}"$)
    Log(LastException)
End Sub

I appear to be really stuck with this :(
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
Yes I do.

I see PushToken: 1

and then underneath that I see

<B4IExceptionWrapper: (null)>

Push notifications while the app is in the foreground work, but not in the back ground
 
Last edited:
Upvote 0

Darren69

Member
Licensed User
Longtime User
Screen shot below

upload_2018-6-6_10-2-38.png
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
and then, just for a laugh, I tried using the APN auth Keys instead of the certificate file - same thing - I get messages when the application is in the foreground, but nothing when its in the background or closed, no notifications at all.

When I reopen the push test app on the iPhone in come all the messages that were sent but never delivered.
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
Sure - I am using B4J and its just the file from the forums with my API key added.

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
   'Below is the server key for the Test App for the iOS only version
    Private const API_KEY As String = "AAAAQT1yBHU:APA91bHUiPBM1zNiSFkfKp4FhNfyMD_4ZP6tc3SYY98g1i9qYx8jaVLclZAYHhgOUkm7BkGsU1tHKojJC-aCI8xK98vScCYVKYS5hjWQYLLTkxuC43hIYpRvUiOcaCSIhlJXrnOkDNuL"

End Sub

Sub AppStart (Args() As String)
    SendMessage("ios_general", "Test", "This is a test message")

    
    StartMessageLoop
End Sub

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)
    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")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    
    Log("Sent string - "&jg.ToString)
End Sub


Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    StopMessageLoop '<-- non ui app only
End Sub
 
Upvote 0

Darren69

Member
Licensed User
Longtime User
@Erel, thank you SO MUCH for holding my hand through this - yes this line 'Use this sending tool instead of the one from the B4A tutorial. This one will work with both B4A and B4i.' made me realise I was using the wrong sender, I had incorrectly assumed that I had from getting it working on Android would be able to send to both platforms from one sender, so it was the ONE thing I had not tried. Always the way really.

I think also trying to work on this after a full day at work didn't help.

I am sure that the additional line will help others if they get stuck on stupid like I did.
 
Upvote 0
Top