B4A Library FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)

Status
Not open for further replies.

Clarification: The nice thing about FCM is that your app doesn't need to run in order to receive messages. The FirebaseMessaging service will be started by the OS when a new message arrives.
It is not a real issue, but if the user closed your app with a "force close" from the device settings then messages will not arrive, until the app is started again.
On some misbehaving devices, killing the app with a swipe will also cause messages not to arrive, until the app is started again.

Firebase Cloud Messaging service is a layer above Google Cloud Messaging service.
It makes it simple to add support for push messages.

Sending messages is done with a simple HTTP request. It is also possible to send message from the Firebase console, though it is not very useful and is actually more complicated than using the REST api.

1. The first step is to follow the Firebase integration tutorial:
https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

Make sure to add the Notifications snippet.
You should also reference FirebaseAnalytics

2. Add a Service named FirebaseMessaging to your app (must be this name):
B4X:
Sub Process_Globals
   Private fm As FirebaseMessaging
End Sub

Sub Service_Create
   fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
   If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
   Sleep(0)
   Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
   n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

fm_MessageArrived will be raised whenever a message is received. In this case we show a notification. You can do whatever you need.

We call SubscribeToTopics from the starter service to make sure that the app will be subscribed when it starts:
B4X:
'Starter service
Sub Process_Globals

End Sub

Sub Service_Create
   CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

Now we can send messages to a "topic" and all the subscribed devices will receive it.

See the code in the attached B4J tool. Note that the API_KEY should be set in the B4J code. It shouldn't be distributed in your app.

API_KEY is the server key from:

SS-2017-04-07_08.10.47.png


A simple non-ui B4J program is attached.

Note that messages sent from Firebase Console will not arrive in some cases. Use the B4J code to test it.
 

Attachments

  • B4J-SendingTool.zip
    1.1 KB · Views: 2,934
Last edited:

mariobam

Member
Licensed User
Longtime User
I can not create bigtextstyle notifications from firebasemessagingservice, fm_MessageArrived, (when my app this closed) .. what do I do?
 

ilan

Expert
Licensed User
Longtime User

ilan

Expert
Licensed User
Longtime User

Alpandino

Member
Licensed User
Hi Erel,

I did all the steps in this tutorial and everything works fine.
However, I noticed that the device that sends the message also receive the message just sent. Is it possible to avoid this? I don't need to receive a message that I have sent.

Thanks
 

ilan

Expert
Licensed User
Longtime User
Hi Erel,

I did all the steps in this tutorial and everything works fine.
However, I noticed that the device that sends the message also receive the message just sent. Is it possible to avoid this? I don't need to receive a message that I have sent.

Thanks

Just dont subscribe to the topic with that device.and it should not recive that notification..

B4X:
fm.SubscribeToTopic("general")
 

Alpandino

Member
Licensed User
Just dont subscribe to the topic with that device.and it should not recive that notification..

B4X:
fm.SubscribeToTopic("general")

I need to subscribe my device to this topic, because I want to receive messages from other users of this topic, but I don't want to receive messages sent from my device. In this case I think that I may find a workaround, for example: I could insert in every message a "sender" ID, so if sender == mysender_ID then ignore this message.
It's a waste of time processing and of network traffic, but it seem that is not possible to find other solutions.

Anybody have some ideas?
 

ilan

Expert
Licensed User
Longtime User
You can create a txt file with the msg u sent and when u receive a msg u check if such a file exists that contains the same text. If true dont show notification.

Thhis should be simpler to implement then create user and handle a db where all users are listed to avoid give two users the same user id....
 

Alpandino

Member
Licensed User
You can create a txt file with the msg u sent and when u receive a msg u check if such a file exists that contains the same text. If true dont show notification.

Thhis should be simpler to implement then create user and handle a db where all users are listed to avoid give two users the same user id....

I must not work with ID users stored in a DB Table, because I'm using the UID provided by Firebase Authentication, so I'm sure this UID is really unique.

Anyway, thank you for the smart suggestion, it can be useful in other situations.
However it seems really strange that Firebase don't give the possibility to avoid the delivery of the message at sender.
 

VTSinLincoln

Member
Licensed User
I'm using FCM notification in an alarm notification app. One of the things I'm noticing though is a very inconsistent delivery time - anything from a few seconds to over 10 minutes.

I've set the messages to be delivered high priority (see https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message) which seems to help. Is there anything else I can do to ensure the notifications turn up in a more timely fashion? Any suggestions where to start looking for delays - the API end (I always get a valid ID from the FCM API when I send messages to it) or the device end?

Vaughan
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi I have done a similar App see here, and for the moment I have quasi real time notifications.
 
Last edited:
Status
Not open for further replies.
Top