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:

cloner7801

Active Member
Licensed User
Longtime User
In ios 10 , we can send a media (image) near push and if 3d touch the notifcation that image show.
Is this support send media ?
 

JohnnyHamburg

Member
Licensed User
Longtime User
Why do we have to put "ios_" prefix to the topicname?
I have no problems sending and receiving messages between android and iphone without the "ios_"-prefix.
 

sergiones

Member
Licensed User
Longtime User
I'm unable to receive the push notification. I did all the steps (actually I revoked all certificates and created new ones from scratch), the Sub Application_RemoteNotification is not being called.

fm_FCMConnected works, I'm using fm.SubscribeToTopic("ios_mySubject") and I can see this topic on Firebase console.

I sent a few messages for this topic using firebase, firebase says the message was sent but no activity on RemoteNotification Sub with a message box.

I'm using B4i version 3.01 with iOs 10.0.

Am I missing something?

Thank you.
 

JohnnyHamburg

Member
Licensed User
Longtime User
I sent a few messages for this topic using firebase

I'm a beginner too, but Erel mentioned quite often that we have to use the B4J-app from the first post to send messages. NOT firebase!
I send with my app from iphone or android. You can receive your own messages.
 

sergiones

Member
Licensed User
Longtime User
I'm a beginner too, but Erel mentioned quite often that we have to use the B4J-app from the first post to send messages. NOT firebase!
I send with my app from iphone or android. You can receive your own messages.

You are right, thanks. I don't remember something so unproductive as iOs...
 

JohnnyHamburg

Member
Licensed User
Longtime User
I have to ask again about the difference to use the "ios_" prefix in the topic. Is it just for the notification, that is handled another way than android?
I send only data between the devices and it works without prefix.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the code:
B4X:
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)
   End If
   m.Put("data", data)

It is required for the notification on iOS.
 

Alpandino

Member
Licensed User
This is the code:
B4X:
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)
   End If
   m.Put("data", data)

It is required for the notification on iOS.


So, If I send a notification from an Android App I must insert the code
B4X:
     m.Put("notification", iosalert)
     m.Put("priority", 10)

in my send message SubRoutine in order to create a notification in the iPhone App?
But in this case it's not needed the prefix "ios_" in the Topic, right?
Other question: what consequences has the field "notification" for an Android App receiveing this kind of message?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
in my send message SubRoutine in order to create a notification in the iPhone App?
Yes.

But in this case it's not needed the prefix "ios_" in the Topic, right?
No, assuming that the device was subscribed to a topic that doesn't start with ios_.

Other question: what consequences has the field "notification" for an Android App receiveing this kind of message?
I don't 100% remember. You will need to check it.
 

Alpandino

Member
Licensed User
I don't 100% remember. You will need to check it.

I checked it: If I put the notification payload in the message, Android redirect this payload on the system tray if the App is in background, otherwise the app will consider only the data payload.
Reading this I noticed "on iOS 10 devices, you'll need to handle applicationReceivedRemoteMessage", but I can't see this Sub in the code of the first post. I can only see the code for notifications:
Sub Application_RemoteNotification

Someone can explain why? I think that without the "applicationReceivedRemoteMessage" Sub is not possible to receive any message....
 

Daniel Uribe

Member
Licensed User
Longtime User
Hi everyone!!

Good morning, If we want to show a specific module when the user click the notification, is possible?

Thanks a lot!!!
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I just started with Notifications using the example code in the first post and noticed something strange:

- when the app is active and I send a notification, the event Application_RemoteNotificationis triggered showing the details of the notification.

- when the app is in the background and I send a notification, the notification shows. When I select the notification, the app is started and the event Application_RemoteNotification is triggered.

- when the app is in the background and I send a notification, the notification shows. When I start the app by pushing the icon of the app, the event Application_RemoteNotification is NOT triggered. It is only after selecting the notification that the event is triggered.

Is this normal or am I doing something wrong?
 
Status
Not open for further replies.
Top