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,244
Last edited:

tufanv

Expert
Licensed User
Longtime 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.

can this pushtoken change ? Or is it always the same for a single user ?
 

tufanv

Expert
Licensed User
Longtime User
It can change.
So how can I store every users token for customized notifications , I need to send a notification to a single user if some thing happens , and I store each users token in db.
 

b4auser1

Well-Known Member
Licensed User
Longtime User
You can save to the db not token, but personal unique name of user, f.e. username or email. App should subscribe to both topics: common for all users and personal (unique) for the user with name unique. If you need to send message to all users, then use general topic name, otherwise - personal (unique) for the user topic name.
 

tufanv

Expert
Licensed User
Longtime User
You can save to the db not token, but personal unique name of user, f.e. username or email. App should subscribe to both topics: common for all users and personal (unique) for the user with name unique. If you need to send message to all users, then use general topic name, otherwise - personal (unique) for the user topic name.
it would make too many topics , lets say we have 1000 active users , wouldn't be it a problem ?
 

b4auser1

Well-Known Member
Licensed User
Longtime User
it would make too many topics , lets say we have 1000 active users , wouldn't be it a problem ?

"There is no limitation on the number of topics or subscriptions. There was a limitation of 1 million subscriptions for the first year after topics was initially launched but that restriction was removed at this year's (2016) Google I/O. FCM now supports unlimited topics and subscribers per app. See the docs for confirmation."
https://stackoverflow.com/questions...er-of-topics-a-device-can-subscribe-to-in-fcm

"Topic messaging supports unlimited topics and subscriptions for each app."
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging
 

hongbii khaw

Member
Licensed User
Longtime User
Hi all,
I got an error message:
ld: framework not found FirebaseMessaging
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Error: ** BUILD FAILED **


The following build commands failed:
Ld build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/SCloud normal armv7
Ld build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/SCloud normal arm64
(2 failures)
What can i do?
Thank you.
 

hongbii khaw

Member
Licensed User
Longtime User
I thought sending tool will be same as the android thing.!!!
Am sorry now it works..
So iOS sending tool have more data fields in the json write?..and that's the issue I was facing..!
Hi,
I think I have the same issue with u since I'm not using the b4j sending tools.
Can you share with me how you solve this problem?
Thank you.
 

dealsmonkey

Active Member
Licensed User
Longtime User
Quick question, please !

When I create an Apple Push Services Production Certificate, it is not showing when I try to create a distribution ad-hoc distribution profile? Only the ios Distribution certificates are available.

Any help would be greatly appreciated, as I am tearing out what hair I have left, getting the notifications to work !!

regards

Neil
 

dealsmonkey

Active Member
Licensed User
Longtime User
See the video tutorial. The provision profile goes with the distribution certificate note with the SSL certificate.

Thanks Erel, I have watched it a few times, but will do so again! Am I right in saying then, that the Apple Push certificate is only used to download and create the p12 file ?
 
Status
Not open for further replies.
Top