iOS Question Silent Push Notifications

Discussion in 'iOS Questions' started by iCAB, Jan 3, 2018.

  1. iCAB

    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why do you want to use silent push notifications? If you just want to show a notification then you should use regular notifications. They are more reliable.
     
  3. 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.
     
  4. Erel

    Erel Administrator Staff 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.
     
  5. iCAB

    iCAB Well-Known Member Licensed User

    Hi Erel,
    Did you mean I should be able to receive regular firebase push notifications after the app is closed?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  7. iCAB

    iCAB Well-Known Member Licensed User

    Then I must be doing something wrong.
    I'll connect iReleaseLogger and check what's happening first.

    Thank you
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    iReleaseLogger will not help. The app is not started when you send a regular push message.
     
  9. iCAB

    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)

    Code:
    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)
    Code:
    fm.FCMDisconnect

    Here is the B4J code being used

    Code:
    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

    Code:
    '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(TrueFalse))
      
        fba.Initialize

        App.RegisterUserNotifications(
    TrueTrueTrue)
        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: Jan 4, 2018
  10. Erel

    Erel Administrator Staff Member Licensed User

    Start without the content_available key. Do you see the system notifications?
     
  11. iCAB

    iCAB Well-Known Member Licensed User

    Should I include m.Put("notification", iosalert)?
     
  12. iCAB

    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
     
  13. Erel

    Erel Administrator Staff Member Licensed User

  14. iCAB

    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

    Code:
    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: Jan 4, 2018
  15. iCAB

    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?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    This is the expected behavior. The notification is shown by the system. Your app is not started.

    Only silent push messages start your app. However they are less reliable (by Apple design).
     
  17. iCAB

    iCAB Well-Known Member Licensed User


    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
    Code:
    Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)

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

    Erel Administrator Staff Member Licensed User

    Two types of notifications:

    - Regular notifications: reliable. Do not start your app. Only show notification. You will not see anything in the logs.
    - Silent notifications: unreliable. They do start your app and you will see the message with iReleaseLogger.
     
  19. iCAB

    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
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    I may have missed something as this thread is too long but I'm not sure what is the problem. It is expected that not all silent notifications will be received.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice