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

Status
Not open for further replies.
Updated tutorial: https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/


Clarification: The nice thing about FCM is that your app doesn't need to run in order to receive messages. The FirebaseMessaging receiver 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 Receiver named FirebaseMessaging to your app (must be this name):
B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general")
End Sub

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

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
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: 919
Last edited:

tigrot

Well-Known Member
Licensed User
Longtime User
For other OS I use a second App which starts when the first terminates, wait for a little time and then restarts the first App again. I don't know if if an App can restart itself after termination, also "booking" this action before termination. Is this possibile in OS Android?
 

Seneca

Active Member
Licensed User
On some devices, applications cannot be started from a push notification after they were explicitly killed by the user.

I mean not to start the app from a push notification. But after killing the app and restart it no longer receives any notification that had been sent while the app was killed.
 

DonManfred

Expert
Licensed User
Longtime User
I don't know if if an App can restart itself after termination, also "booking" this action before termination. Is this possibile in OS Android?
There is no Api to restart your App.
For other OS I use a second App which starts when the first terminates, wait for a little time and then restarts the first App again.
In Android (at least in newer Versions) you are not able to see the other app is actually terminated. So, you can´t react on this.
 

Seneca

Active Member
Licensed User
When you restart the app do you also restart the firebase service?

After restarting, the Firebase service starts successfully. In fact, the messages I send after restarting the app are received correctly. Messages that aren't received are those that were sent while the app was killed.

Attached screenshot with services, before and after. They are the same.


Test_FirebaseNotifications_2.jpg
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This fits the situation I described. On some devices when you kill an app with a swipe, the app goes to the stop state and ignores all notifications until the app is explicitly started again. This is not the common behavior.

You shouldn't however expect all messages to be received. Instead contact your server when a message arrives and download whichever information you need (or do it when the app starts).
 

Seneca

Active Member
Licensed User
And is it normal that after restarting the app explicitly does not receive the messages that were sent while it was killed, but does it receive the messages that are sent from that moment?
 

uniplan

Active Member
Licensed User
Longtime User
Hi, i followed this example to sent a notification push.
I'm not included the last part where you can send the message to topic.
I'm interested only on the part where you can sent the message from firebase to all devices.

I test it and i receive the notification push, but the title and body are null.
you can help me about this problem?
Thank you @Erel
 

tznikos

Member
Licensed User
Longtime User
Hi,

I face the same problem with uniplan,

When the application is in the foreground, i receive the notification push, but the title and body are null.
If the application is in the Background or is closed then receive the message correct,

can anyone know why happen that ?
 
Last edited:
Status
Not open for further replies.
Top