iOS Tutorial FirebaseNotifications / Push Messages (server not required)

Status
Not open for further replies.
Updated tutorial: https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/
The configuration steps are still relevant.


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.
SS-2016-07-04_17.19.12.png


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.

Update: It is simpler to use the new and recommended APN authentication keys: https://www.b4x.com/android/forum/t...on-keys-vs-authentication-certificates.126402

Old keys (works as well):
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:

SS-2016-07-04_17.25.02.png


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:

SS-2016-07-04_17.35.51.png

You only need to upload the production APN.

5. Download GoogleService-Info.plist and copy it to Files\Special folder.

Code

The code should be similar to:
B4X:
#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(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

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_.
 

Attachments

  • SendingTool.zip
    1 KB · Views: 2,131
Last edited:

valentino s

Active Member
Licensed User
Longtime User
If I have more than one aps*.cer file in the key folder I can't create a filebase service and the file firebase_push.p12

This is the error.

2016-09-13_110708.jpg


If I delete all aps*.cer files except one, it works.

So I can I manage multiples aps_ files for different projects ?

This happens because when I create a push notifications certificate and, Apple let me download a file called aps.cer and not aps_name.cer ?


Thanks
 

fabio borges

Member
Licensed User
Longtime User
Hi guys,
my notification is not being sent and I cant know why.
When I send notification from Firebase console to general, this works, but not by token.
So, I'm trying to send notification direct of the ios application for firebase using Java, with the following library (https://github.com/Raudius/Pushraven) but I dont really know if this is working, there is any way of knowing if the notification is getting in the Firebase?

And assuming that the notification is in Firebase, I'm doubts whether my application in B4i is working properly.

Below is the log when I run the application with B4iBridge.
I am using the certificate in the development mode.

Application_Start
The default app has not Been configured yet.
Configuring the default app.
Application_Active
FCMConnect
HERE
Application_Pushtoken
token = 384CB0332C *
Application_Pushtoken
token = 384CB0332 *
Application_Pushtoken
token = 384CB0332C9C *
Error response:, status code: 0
Error uploading token
Class (b4i_httpjob) instance released.
Error response:, status code: 0
Error uploading token
Class (b4i_httpjob) instance released.
Error response:, status code: 0
Error uploading token
Class (b4i_httpjob) instance released.
FCMConnected
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile /
http://192.168.0.102:8084/ZipWeb/mobile/
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile / home /
http://192.168.0.102:8084/ZipWeb/mobile/home/
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile / authCookie
http://192.168.0.102:8084/ZipWeb/mobile/authCookie
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile / home /
http://192.168.0.102:8084/ZipWeb/mobile/home/
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile / home / # / ZipWeb / mobile / budgets
OVERhttp: //192.168.0.102: 8084 / ZipWeb / mobile / # budgets / ZipWeb / mobile / home
Application_Inactive
Application_Background
FCMDisconnect
Application_Foreground
Application_Active
FCMConnect
FCMConnected


Some hint of what else should I check?
Thank you!
 

fabio borges

Member
Licensed User
Longtime User
I just checked that the Firebase connection is returning code 200, so, is probably something with my b4i application.

How can I check it?
I'm sending a attached with my Main Application, so you can check.

Thank you!!
 

Attachments

  • ZipPharma.zip
    5.8 KB · Views: 526

fabio borges

Member
Licensed User
Longtime User
Ok, did it, I just changed the API_KEY in the b4j file, and I got the following log:

Program started.
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@1410acf, tag=java.lang.Object@1089e5f
, main=null, httputils2service=null]
{"message_id":8090239757702940595}


But the notification isn't reaching my phone!
 

andyr00d

Member
Licensed User
Longtime User
ok I've tried this 3 times now... At the end when I upload the .p12 file to Firebase console, I am getting: The Bundle ID in the certificate does not match the Bundle ID you entered.

I've followed the steps carefully, is it the package name in build configurations that is causing it?
To be clear: I've copied the push.mobileprovision and aps.cer files to my keys folder
 

andyr00d

Member
Licensed User
Longtime User
The package name in build configurations is the same as my Firebase iOS project name... any other suggestions?
 

andyr00d

Member
Licensed User
Longtime User
Ah that worked! it had a "1" after the name, so I recreated it all and now it's fine. Thanks!
 

tufanv

Expert
Licensed User
Longtime User
my app started not receiving push messages as notifications while on background. Only when i open the app it is coming as msgbox. 2 days ago everything was working fine . I am sure that i use
App.RegisterUserNotifications(True, True, True) 'allow badge, sound and alert
and i allow it. I tried at different devices also. What may be the problem to make it stop working suddenly =
 

tufanv

Expert
Licensed User
Longtime User
Implement this sub and see whether there are any errors:
B4X:
Sub Application_PushToken (Success As Boolean, Token() As Byte)
   Log($"PushToken: ${Success}"$)
   Log(LastException)
End Sub
it says push token : 0 could not find a aps-environment .

But why did this start hapening suddenly ? what to do to fix it ?
 
Status
Not open for further replies.
Top