iOS Tutorial FirebaseNotifications / Push Messages (server not required)

Discussion in 'iOS Tutorials' started by Erel, Jul 5, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Firebase Notifications service makes it relatively easy to send push messages.

    Integrating Firebase: https://www.b4x.com/android/forum/threads/firebase-integration.68623/

    Push messages in iOS requires some configuration.


    1. Create a new explicit (non-wildcard) App ID with the package name of the push app. For example anywheresoftware.b4i.push. Enable push notification service.
    2. Create an Apple Push Notification SSL certificate. Use the same certSigningRequest.csr file that you previously created.
    This can be done from App IDs - Choose the id - Edit.
    [​IMG]

    I recommend using a Production SSL Certificate with a Distribution / Ad Hoc provision profile. This way you can use the same tokens during development and in production.

    3. Create a provision file with the new App ID.

    4. There should be a file named aps_*.cer in the keys folder. Now you should click on Tools - Build Server - Create Push Key - Firebase Service:

    [​IMG]

    This will create a file named firebase_push.p12 in the keys folder. You need to upload it to Firebase console under Settings - CLOUD MESSAGING:

    [​IMG]
    You only need to upload the production APN.

    Code

    The code should be similar to:
    Code:
    #Entitlement: <key>aps-environment</key><string>production</string>
    'use the distribution certificate
    #CertificateFile: ios_distribution.cer
    'use the provision profile that goes with the explicit App Id
    #ProvisionFile: Firebase.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
    Use the attached B4J (non-ui) program to send messages. It handles ios messages a bit differently. There is an assumption that all iOS topics start with ios_.
     

    Attached Files:

    Last edited: Oct 24, 2016
  2. aarroyo

    aarroyo Member Licensed User

    as I can get the token fm , to send individual messages.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    You can handle the Application_PushToken event if you want to access the token. Another option is to create a unique id for each device.
     
  4. Keith Yong

    Keith Yong Member Licensed User

    HI Ere,
    is it this one? but when I try to push manual with the token id, it failed.

    Code:
    Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
    tokenID = bc.HexFromBytes(Token)
    End Sub
    Screen Shot 2016-07-05 at 5.07.12 PM.png
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Are you able to send to a topic with the B4J code?
     
  6. Keith Yong

    Keith Yong Member Licensed User

    it works if i send topic with "ios_general", not for token
     
  7. aarroyo

    aarroyo Member Licensed User

    It happens to me the same thing when I send a single message with the token that gives me Application_PushToken in the console firebase this tells me error, but since B4A with fm.token, it works correctly.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to get the token:
    Code:
    Private Sub GetToken As String
       
    Dim no As NativeObject
       
    Dim token As NativeObject = no.Initialize("FIRInstanceID").RunMethod("instanceID"Null).RunMethod("token"Null)
       
    If token.IsInitialized Then Return token.AsString Else Return ""
    End Sub
    Call it from FCMConnected event. Note that there are cases where the token is not available.
     
    dealsmonkey and Keith Yong like this.
  9. tufanv

    tufanv Well-Known Member Licensed User

    I am having a little problem with files. I did as tutorail said.
    1) I put the provision file of push under keysstore folder ) the folder for distrubition files ) is that right =?
    2) To test on my device do i need to use distrubiton certificate or development ?= ( normally we use developemnt certificate but in tutorail you say use distribution )

    Trying the distrubution certificate and push provision gives me a error of :

    Check dependencies
    Code Sign error: No codesigning identities found: No codesigning identities (i.e. certificate and private key pairs) that match the provisioning profile specified in your build settings (“airlinepush”) were found.


     
  10. Erel

    Erel Administrator Staff Member Licensed User

    1) I use a single folder for all keys.
    2) Yes, you should use a distribution certificate. It behaves exactly the same as development certificates.
     
  11. tufanv

    tufanv Well-Known Member Licensed User

    With this config i can build a release one but i cant build a debug so i cant watch the logs . Also with a build release, at the startup my app does not ask if i allow push notifications. Am I missing stg =?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Yes. You can build debug applications as well.
     
  13. tufanv

    tufanv Well-Known Member Licensed User

    OK. I will revoke all the certificates and create new ones. Can it affect anything badly for my app currenltly online ? For example one of them is using a push service .
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    You don't need to revoke the existing keys. It will cause the push service to stop working. Create a new provision profile.
     
  15. tufanv

    tufanv Well-Known Member Licensed User

    Now i can run in debug mode. I can see the fcm connected log. My problem is on the first run the app does not ask me if i allow the push notifications . Is it normal ? I tried to send a msg but couldnt receive but i beleive it is normal because i did not see the allow notification window.

    When I launch the app i can see the push message via msgbox but it does not come as push also.
     
    Last edited: Jul 6, 2016
  16. Erel

    Erel Administrator Staff Member Licensed User

    Try to uninstall the app and install it again. You have probably already approved the app in the past.

    This is how push messages work. You will only see the notification if the app is in the background.

    For further discussions please start a new thread.
     
  17. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    Is there any reason to do not have a token?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    It depends on your usage. If you are only sending to topics then you don't need it (which means that you don't need to run an online server).
     
  19. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    That's not my case. Definitely I would need to send user specific messages :)

    One other question. Is it possible to use the certificates and the provision file I already generated to use with the old notifications solutions for B4i?

    Thanks.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
Loading...