iOS Question B4i - Firebase Push Notification - Clarification

Discussion in 'iOS Questions' started by Darren69, May 22, 2018.

  1. Darren69

    Darren69 Member Licensed 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,
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
    Darren69 and BillMeyer like this.
  3. Darren69

    Darren69 Member Licensed 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.
     
  4. Darren69

    Darren69 Member Licensed 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
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    This doesn't mean too much as the messages are not sent with APN when your app is in the foreground.

    I can only guess that you missed a step. Try to start from scratch with a new project.
     
  6. Darren69

    Darren69 Member Licensed 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.

    Code:
    #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(
    TrueTrueTrue)
        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 :(
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Do you see the PuskToken: xxx message in the logs when you start your app?
     
  8. Darren69

    Darren69 Member Licensed 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: Jun 5, 2018
  9. Erel

    Erel Administrator Staff Member Licensed User

    I understand. As I wrote it doesn't mean much.

    Can you post a screenshot of the Firebase certificate page?
     
  10. Darren69

    Darren69 Member Licensed User

    Screen shot below

    upload_2018-6-6_10-2-38.png
     
  11. Darren69

    Darren69 Member Licensed 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.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code that you are using to send the messages?
     
  13. Darren69

    Darren69 Member Licensed User

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

    Code:
    '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
     
  14. Erel

    Erel Administrator Staff Member Licensed User

  15. Darren69

    Darren69 Member Licensed 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.
     
    Erel likes this.
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