Android Tutorial [B4X] Firebase Push Notifications 2023+

Status
Not open for further replies.
Google has deprecated the previous method of sending push notifications. It will stop working on June 2024.
If you have an already working solution then you only need to update the B4J side. Do make sure that you switched from the service to a receiver on B4A.
The new API is (confusingly) named FCM API v1.

B4J

1. Make sure that the new API is enabled as shown in this screenshot:

1687781823038.png


2. Project settings - Service accounts - Generate new private sign key. The browser will download a json file. This file includes a private key that gives access to your firebase account. Keep it secured.
This file is used by the sending tool.
3. Find the project id under the General tab.
4. Download the dependencies and unpack in B4J additional libraries folder: https://www.b4x.com/b4j/files/b4j-push-deps.zip
5. Download B4J-SendingTool.zip. Update ProjectId and ServiceAccountFilePath.

B4A

1. Follow the generic Firebase integration tutorial: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
Download google-services.json and put it in the project folder.
2. See the attached project.
3. The notification is created in FirebaseMessaging receiver. Note that in debug mode, it will only work while the app is running.

B4i

1. Follow the configuration instructions: https://www.b4x.com/android/forum/t...h-messages-server-not-required.68645/#content (ignore the code references).
2. Download the attached project. Note the two subs added there (RemoteNotification and PushToken).

Notes

1. In iOS the notification popup only appears while the app is not in the foreground. You can UserNotificationCenter to change this behavior (search and you will find).
2. FirebaseMessaging module isn't shared between the two projects. It is different in B4A and B4i.
3. The message payload can be customized in many ways. API reference: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
 

Attachments

  • PushClients.zip
    16 KB · Views: 1,431
  • B4J-SendingTool.zip
    1.8 KB · Views: 1,062
Last edited:

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
For those of us who use a PHP server of some type for the sending you can use the code from here :


I had to tweak it a bit to work with Laravel.

I use the same topic for iOS and Android, so my data structure looks like this.
B4X:
        $data = ['title'=>$title,
                 'body'=>$body];

  
        $message = ['topic'=>$topic,
                    'data'=>$data,
                    'android'=>['priority'=>'high'],
                    'notification'=>$data,
                    'apns'=>['headers'=>['apns-priority'=>'5'],
                             'payload'=>['aps'=>['sound'=>'default',
                                                 'badge'=>0]
                                        ]
                            ]
                    ];
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is much more difficult to directly send the request from B4R, with the new API. You can see in the B4J code that it relies on Google's SDK for the token handling. There is no similar SDK for Arduino.

The best way forward is to have a B4J server that receives a message from the B4R device and sends the message to Firebase.
 

derez

Expert
Licensed User
Longtime User
The best way forward is to have a B4J server that receives a message from the B4R device and sends the message to Firebase.
Amazing, I thought the same in the case that your answer will be like that !
 

FrostCodes

Active Member
Licensed User
Longtime User
If you use PHP for sending your notifications and need a full working sample for B4A/B4I, check here:

 

derez

Expert
Licensed User
Longtime User
How to send notifications from b4a app ? the current "SendMessage" sub still works but I would prefer to change it to the new FCM (in a year I won't remember what and why I had done). The solution for B4R will not work for me because the notification should be sent when the b4j server is down.
Thanks.
 

derez

Expert
Licensed User
Longtime User
Try to use the B4J code with the dependencies. I don't know whether the dependencies are compatible with Android. They might.
It works. I had to put the json file in DirAssets and copy it to DirInternal.
 

derez

Expert
Licensed User
Longtime User
I assume that you are making a private app
Correct. Possible way to secure it is to encript the file that goes to DirAssets and then let the app decript it before using it.
A way of "encription" can be to use jsonparser, create a map from the file and save the map as RAF which will be put in DirAssets, the app will do the reverse from the RAF to json file.
 
Last edited:

marcick

Well-Known Member
Licensed User
Longtime User
I worry to make disaster in migrating to the new method. I actually have on the field hundreds of B4I and B4A apps that receive notification from my B4J server with the actual method.
If I first update the B4A and B4I apps, will they continue to receive notification with the actual B4J code or everything should be simultaneous with the B4J update ?
 

marcick

Well-Known Member
Licensed User
Longtime User
The change is only relevant for the B4J side. No change is needed on the clients.
Thanks. Because I read “Do make sure that you switched from the service to a receiver on B4A.” I was thinking that the new B4J code works only with the updated B4A apps.
 

derez

Expert
Licensed User
Longtime User
Is it possible to get the notifications by b4j app and show them in win10 or win11 ?
 
Status
Not open for further replies.
Top