iOS Tutorial FirebaseNotifications / Push Messages (server not required)

Status
Not open for further replies.

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

dealsmonkey

Active Member
Licensed User
Yes. What happens when you run it? Do you see the push token logs?

In the fcm_connected is subscribe to ios+general, I am getting a PushToken:1 from the log and a token is being generated. If I send from firebase it shows as completed, but no devices.

My b4j app log is this :

B4X:
Waiting for debugger to connect...
Program started.
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@133314b, tag=java.lang.Object@b1bc7ed
, httputils2service=null]
{"message_id":7452680364343505248}

Thanks in advance.
 

dealsmonkey

Active Member
Licensed User
You mean ios_general, right?


This is not relevant. Don't use Firebase console.

The message was sent successfully.
Are you using the exact code from the tutorial?

Have you tested it in both background and foreground modes?

Ok, thanks. I have checked the code but will do so again. I have tried foreground and background. I will keep checking, thanks for your input, it is appreciated.

Neil
 

dealsmonkey

Active Member
Licensed User
Just one thing is, you mentioned in an earlier post about checking the aps.cer file and the bundle ID. When I look in the aps.cer, my bundle
Ok, thanks. I have checked the code but will do so again. I have tried foreground and background. I will keep checking, thanks for your input, it is appreciated.

Neil

Ok, I have sorted out my problem :oops::oops::oops: = analytics.Initialize

I had forgotten to initialize the analytics. As soon as I did the messages came through :)

The good thing about this, is I now understand the Apple Certificate, provisioning system a lot better !!!!

Thanks for your help again.

Regards

Neil
 

Melek

Member
Licensed User
Enviando datos al compilador remoto. Error
Out: Build settings from command line:
ARCHS = arm64
CODE_SIGN_IDENTITY = iPhone Distribution: Inversiones Alzir S DE RL (QZ4V5V6D72)
CONFIGURATION_BUILD_DIR = /Users/administrator/Documents/UploadedProjects/<user id>/Payload
OTHER_CODE_SIGN_FLAGS = --keychain <user id>
PRODUCT_NAME = Ferinpro 2019
PROVISIONING_PROFILE = f46fb132-e8f2-4a62-8be1-ab02887a87c8

note: Using new build system
note: Planning build
note: Constructing build description
CreateBuildDirectory /Users/administrator/Documents/UploadedProjects/<user id>/build (in target: B4iProject)
cd /Users/administrator/Documents/UploadedProjects/<user id>
builtin-create-build-directory /Users/administrator/Documents/UploadedProjects/<user id>/build

MkDir /Users/administrator/Documents/UploadedProjects/<user id>/Payload/Ferinpro\ 2019.app (in target: B4iProject)
cd /Users/administrator/Documents/UploadedProjects/<user id>
/bin/mkdir -p /Users/administrator/Documents/UploadedProjects/<user id>/Payload/Ferinpro\ 2019.app

WriteAuxiliaryFile /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources/Entitlements.plist (in target: B4iProject)
cd /Users/administrator/Documents/UploadedProjects/<user id>
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources/Entitlements.plist

ProcessProductPackaging /Users/administrator/Library/MobileDevice/Provisioning\ Profiles/f46fb132-e8f2-4a62-8be1-ab02887a87c8.mobileprovision /Users/administrator/Documents/UploadedProjects/<user id>/Payload/Ferinpro\ 2019.app/embedded.mobileprovision (in target: B4iProject)
cd /Users/administrator/Documents/UploadedProjects/<user id>
builtin-productPackagingUtility /Users/administrator/Library/MobileDevice/Provisioning\ Profiles/f46fb132-e8f2-4a62-8be1-ab02887a87c8.mobileprovision -o /Users/administrator/Documents/UploadedProjects/<user id>/Payload/Ferinpro\ 2019.app/embedded.mobileprovision


ld: symbol(s) not found for architecture arm64
clang: error:
linker command failed with exit code 1 (use -v to see invocation)


Error: ** BUILD FAILED **


The following build commands failed:
Ld /Users/administrator/Documents/UploadedProjects/<user id>/Payload/Ferinpro\ 2019.app/Ferinpro\ 2019 normal arm64
(1 failure)
 

Melek

Member
Licensed User
Hi, I have this error when compiling the App, could you help me?
I followed all the steps as in the tutorial
 
Status
Not open for further replies.
Top