Android Tutorial [B4X] Firebase Push Notifications 2023+

Status
Not open for further replies.

derez

Expert
Licensed User
Longtime User
The question isn't clear enough. You cannot receive Firebase notifications on the desktop. You can have a Windows client that is connected to the B4J server and shows notifications.
Because I saw this in "Google cloud messaging" site :
An Apple, Android, or web (JavaScript) client app that receives messages via the corresponding platform-specific transport service.
 

derez

Expert
Licensed User
Longtime User
The instructions are here: https://firebase.google.com/docs/cloud-messaging/js/client
Not trivial but possible with a server and through a browser. Not directly.

If you have your own server than their are simpler solutions.
My solution:
The sending app send a MQTT message in parallel to sending the FB message, to the cloud MQTT that I use already.
A small b4j app runs on the PC and listens to MQTT. Its window is small and out of the screen until a msg is received, then it is shown. Hiding it again is either by the user or by timer.

 

Sandman

Expert
Licensed User
Longtime User
Apologies for going slightly off-topic.
A small b4j app runs on the PC and listens to MQTT. Its window is small and out of the screen until a msg is received, then it is shown. Hiding it again is either by the user or by timer.
This thread might be relevant for you:
 

derez

Expert
Licensed User
Longtime User
Thanks for the pointer but my app seems to me simpler and without any additional libraries or dependencies. I may work a little on the look of it but thats it.
 

GraemeW

Member
Licensed User
Bit slow here so just checking .. my app sends notifications to all its peers by the FCM legacy API via a php script on a shared hosted Unix server. The script stores the API key.

For the new API I'm planning to implement a version of the B4J sending tool script on the Unix server - with the .json file stored on a Unix path. My app will then call the remote js when it needs to send notifications. Does this make sense?
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
If you are using PHP on a server why not use the solution given by @FrostCodes in post 8?
 

GraemeW

Member
Licensed User
I'm still figuring it out but I'm still in dummy class for php - first attempt yielded "The registration token is not a valid FCM registration token"
 

IdasI4A

Active Member
Licensed User
Longtime User
B4J-SendingTools works perfectly in the IDE, but when I make a standalone package, and run the package I get the error:

Can you tell me what I'm doing wrong?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Updated the sending tool with a single addition:
B4X:
#PackagerProperty: AdditionalModuleInfoString = uses com.google.auth.http.HttpTransportFactory;

With this line the standalone packager will work.

Tip: if you want to put the json file in the Files folder to simplify packaging then change GetTokenValue to:
B4X:
Private Sub GetTokenValue (Dir As String, FileName As String) As String
    Dim GoogleCredentials As JavaObject
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
    Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(File.OpenInput(Dir, FileName))) _
        .RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
    Credentials.RunMethod("refreshIfExpired", Null)
    Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub
 

IdasI4A

Active Member
Licensed User
Longtime User
Thank you, thank you very much. It works perfectly
 

dieterp

Active Member
Licensed User
Longtime User
At some point while running the B4J-SendingTool app I get the error below. Should we be handling token refreshing ourselves or is this supposed to be handled in the existing code (It looks like the code should be handling it)

UPDATE: I have implemented a timer to call GetTokenValue(ServiceAccountFilePath) every 30 minutes which has resolved the issue (The token expires after 60 min). It's worth noting that I modified the code into a UI based app from the Non-UI example. I'm not sure if this perhaps changes the behavior of the token refreshing in a different manner

 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…