iOS Question Silent Push Notifications

iCAB

Well-Known Member
Licensed User
Hi All,

I am looking to implement a reliable way to receive notifications when the app is closed.
I looked at the tutorial in the link below
https://www.b4x.com/android/forum/threads/silent-push-notifications.56016/

But when I read the statement below, it makes me wonder how do other apps such as Skype implement their notifications in iOS
iOS will not start the app automatically if the user has killed it. This makes it a bit difficult to test that it really works.


Thanks
iCAB
 

iCAB

Well-Known Member
Licensed User
Hi Erel

Thanks for your reply.
My goal is to be able to invoke functions, such us downloading and playing audio files after the application is killed by the user.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Silent push messages will not work if the user force-killed your app. The same is true for background fetching.

Regular messages will work. However you cannot run anything as a result of such messages.
Hi Erel,
Did you mean I should be able to receive regular firebase push notifications after the app is closed?
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Hi Erel

Can you please let me know what is wrong with what I am doing in here. Somehow background notifications are not working.

Here is what I have done so far:
1. Regenerated all certificates and provision profiles (just in case)
2. I downloaded Sending_tool B4J project to eliminate other possibilities
3. I generated a small test project to eliminate other possibilities as well
4. I am using iReleaseLogger to capture events log, for when the app is in the background
5. I have tried local builder with XCode 9 & hosted builder


Here are the results so far:
1. All push messages arrive while app in Foreground, no problem

2. if I send the app to the background, and if I don't include the line of code below in the sending tool, I see no reaction from the app ( I should see "Message arrived" on iReleaseLogger, but I don't)

B4X:
m.Put("notification", iosalert)

3. If I include, the above line of code in the sending tool, and I send the app to the background. sometimes (not all the time), I will see 2 notifications (system one & the one generated by the app). In other words, "Application_RemoteNotification" is called. The interesting thing, is that after a while only system notifications are showing (meaning "Application_RemoteNotification", is not getting called any more)

4. After Step #3, if I kill the app completely, send it few messages, restart it, sometimes step#3 behaviour repeats

5. I wasn't sure if I should keep this line, in Application_Background (so I tried with and without)
B4X:
fm.FCMDisconnect


Here is the B4J code being used

B4X:
    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)
        m.Put("content_available", True) '<--- add
    End If
    m.Put("data", data)


Here is the B4I Code

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
  
    #Entitlement: <key>aps-environment</key><string>production</string>
  
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #PlistExtra:<key>NSMicrophoneUsageDescription</key><string>Record video Audio</string> 
  
  
    #CertificateFile: ios_distribution.cer
    #ProvisionFile: someapp_non_wild_adhoc.mobileprovision
  
    #PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string><string>audio</string></array>
  
  
#End Region

Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
  
    Dim clsAudioRecord As AudioHandler
  
    Public fba As FirebaseAnalytics
    Public fm As FirebaseMessaging
    Public fbauth As FirebaseAuth
    Public fbstorage As FirebaseStorage
    Private bucket As String = ""
  
    Private hd As HUD
End Sub

Private Sub Application_Start (Nav As NavigationController)
  
    Dim rl As ReleaseLogger
    rl.Initialize("192.168.0.111", 54323)
  
    NavControl = Nav
    LoginModule.Show
  
    'App.ApplicationIconBadgeNumber = 0
    App.ApplicationIconBadgeNumber = 1
    App.ApplicationIconBadgeNumber = 0
  
  
    Dim no As NativeObject = App
    no.RunMethod("setStatusBarHidden:animated:", Array(True, False))
  
    fba.Initialize

    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
  
    fm.Initialize("fm")

    clsAudioRecord.Initialize
  
End Sub



 


Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)

    Log("Message arrived")
  
    RaiseNotification
    CompletionHandler.Complete
End Sub


Private Sub RaiseNotification()
  
'    Msgbox("Hello", "")
'    Return
  
    Dim n As Notification
  
    n.Initialize(DateTime.Now + DateTime.TicksPerSecond)
    n.AlertBody = "hello"
    n.PlaySound = True
    n.Register
  
End Sub


Sub Application_ReceiveLocalNotification (LN As Notification)
    hd.ToastMessageShow("Notification arrived: " & LN.AlertBody, True)
End Sub

Private Sub PlayPreRecordedAudio()
  
    If File.Exists(File.DirTemp, "test.mp4") = True Then
        clsAudioRecord.PlaySound2(File.DirTemp, "test.mp4" )
    End If

End Sub

Private Sub Application_Active
    Log("Application Active")
    fm.FCMConnect
  
End Sub


Private Sub Application_Background
    Log("Application Background")
    fm.FCMDisconnect 
End Sub



Private Sub fm_FCMConnected
    Log("Connected")
    fm.SubscribeToTopic("ios_general")
End Sub

Thank you
 
Last edited:
Upvote 0

iCAB

Well-Known Member
Licensed User
If I exclude m.Put("notification", iosalert), then there is no system notification.
if I include it, then there is.

But the behaviour is still more or less the same. After a minute or so background messages stop arriving
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Hi Erel, I started with the code in the example as is.
As I mentioned, when the application is sent to the background, we see the system notification only as included in the B4JCode

B4X:
Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")

However, I don't see "Message Arrived" on iReleaseLogger desktop application.
As soon as I bring the application back to Foreground, iReleaseLogger shows "Message Arrived" for as many messages as we sent to the topic.
 
Last edited:
Upvote 0

iCAB

Well-Known Member
Licensed User
Hi Erel, I am starting to suspect that the problem is somewhere else and possibly somehow configuration related..

Just for curiosity, I have also tried background Fetch.

I downloaded the example in the last post in this thread:
https://www.b4x.com/android/forum/threads/background-fetch-downloads.56022/page-2, it didn't work either

Is there anything special for background execution that I could be missing ( other than: <key>UIBackgroundModes</key> )?
Does the compiler auto generate info.plist file?
 
Upvote 0

iCAB

Well-Known Member
Licensed User
This is the expected behavior. The notification is shown by the system. Your app is not started.


Hi Erel, I believe that "Message Arrived" should be displayed on iReleaseLogger if the Silent Push is processed,
Isn't that the expected behavior?

Please see below
B4X:
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)

    Log("Message arrived")
 
    RaiseNotification
    CompletionHandler.Complete
End Sub
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Hi Erel
Thanks again for your reply.
I am fully aware of your comments above, and if you read my original post carefully, you will see exactly the above.

Can we try to break this into 3 different pieces:
1. B4I code
2. B4J code
3. App configuration ( I beleive that the issue is in here )

1 & 2 I supplied in here, just to make sure I am doing the right thing and also if I am not mistaken they are exactly the same as the sample code (also I used the B4J sample code as is )..


Please let me know if you can examine 1 & 2 in here, if you are ok with me sending you # 1 (PM) as a sample project with all my keys & profile.

Thank you
 
Upvote 0
Top