iOS Tutorial FirebaseNotifications / Push Messages (server not required)

Status
Not open for further replies.

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.


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:



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:


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

Last edited:

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.
 
Last edited:

Keith Yong

Active Member
Licensed User
HI Ere,
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.
is it this one? but when I try to push manual with the token id, it failed.

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

Erel

Administrator
Staff member
Licensed User
Are you able to send to a topic with the B4J code?
 

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.
 

Erel

Administrator
Staff member
Licensed User
You can use this code to get the token:
B4X:
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.
 

tufanv

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


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.


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:



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:


You only need to upload the production APN.

Code

The code should be similar to:
B4X:
'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)
   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_.
 

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.
 

tufanv

Expert
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.
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 =?
 

tufanv

Expert
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 .
Yes. You can build debug applications as well.
 

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.
 

tufanv

Expert
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.
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:

Erel

Administrator
Staff member
Licensed User
Is it normal ?
Try to uninstall the app and install it again. You have probably already approved the app in the past.

When I launch the app i can see the push message via msgbox but it does not come as push also.
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.
 

Luiz Fernando Orlandini

Active Member
Licensed User
You can use this code to get the token:
B4X:
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.
Is there any reason to do not have a token?
 

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

Luiz Fernando Orlandini

Active 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).
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.
 
Status
Not open for further replies.
Top