Android Tutorial FCM Push-Messages: Foreground/Background/Data/Notification

Erel made a fantastic example how to use Google's FCM Push-Messaging:
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

To complete the example, it is important to know FCM works in detail. Otherwise you wonder why your messages "don't arrive", etc.

IMPORTANT: Check if your app will not be stopped when it will send back to the background or swiped! (See Android settings -> apps -> your app -> energy, etc.)

1. What kind of message do you send to the device (Notification, Data or both)? Yes, this is important!

Each message is sent by the REST API. Here you use a JSON to provide the data you want to send.

Notification (it is just a notification without any data)

B4X:
{
   "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", 'device token
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    }

Data (there is just data, no notification)

B4X:
{
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}

Both (notification AND data)

B4X:
'{
'    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
'    "notification" : {
'      "body" : "great match!",
'      "title" : "Portugal vs. Denmark",
'      "icon" : "myicon"
'    }
'    "data" : {
'      "Nick" : "Mario",
'      "Room" : "PortugalVSDenmark"
'    }
'  }

2. In what state is your App?

Foreground (the app is visible and running)
Background (the app is not visible and is paused OR it has been whiped by the user)

3. Take a look at this table to see how the service (app) behaves:
fcm.JPG


Example:

a) If the App is in the foreground AND you send just a notification, all the data will be available in "onMessageReceived" ONLY (there's no DATA part!)

b) If the App is in the Background AND you send both (notification AND data) the notification will appear automatically in the system tray (also known as THE NOTIFICATION but without sound, etc.). As you can read you don't need to throw an extra notification! When the user clicks on it, the data wil be vailable in the extras of the intent.

c) If your send data only, everything is available in "onMessageReceived" only. So you need to throw a notification on your own.

4. What is"onMessageReceived", "System tray" and "extras of the intent" exactly?

a) onMessageReceived

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)

in the FirebaseMessaging service

b) System tray

Where notifications for the users live

c) extras of the intent

B4X:
Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    Log(in.GetExtra("data"))
End Sub

With

B4X:
Log(in.ExtrasToString)

all "extras" are shown like:

B4X:
Bundle[{google.sent_time=1479841813693, data=8, from=3xxxxxxxx, google.message_id=0:xxxxxxxxxx%yyyyyyyyy, collapse_key=your.package.name}]

I've sent just an "8" as data

in the main activity (= the app will be started "by intent" = Click on the notification)

So now you know how f.e. WhatsApp does it :)

5. Good to know

TO, DATA and NOTIFICATION are keywords! Under this structure you can add maps with any name (key/value) as you like, even 1000 will do (except you don't exceed the max length of 4 KB).
 
Last edited:

Tareq Khan

Member
Licensed User
Longtime User
Erel made a fantastic example how to use Google's FCM Push-Messaging:
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

To complete the example, it is important to know FCM works in detail. Otherwise you wonder why your messages "don't arrive", etc.

IMPORTANT: Check if your app will not be stopped when it will send back to the background or swiped! (See Android settings -> apps -> your app -> energy, etc.)

1. What kind of message do you send to the device (Notification, Data or both)? Yes, this is important!


Each message is sent by the REST API. Here you use a JSON to provide the data you want to send.

Notification (it is just a notification without any data)

B4X:
{
   "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", 'device token
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    }

Data (there is just data, no notification)

B4X:
{
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}

Both (notification AND data)

B4X:
'{
'    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
'    "notification" : {
'      "body" : "great match!",
'      "title" : "Portugal vs. Denmark",
'      "icon" : "myicon"
'    }
'    "data" : {
'      "Nick" : "Mario",
'      "Room" : "PortugalVSDenmark"
'    }
'  }

2. In what state is your App?

Foreground (the app is visible and running)
Background (the app is not visible and is paused OR it has been whiped by the user)

3. Take a look at this table to see how the service (app) behaves:
View attachment 50256

Example:

a) If the App is in the foreground AND you send just a notification, all the data will be available in "onMessageReceived" ONLY (there's no DATA part!)

b) If the App is in the Background AND you send both (notification AND data) the notification will appear automatically in the system tray (also known as THE NOTIFICATION but without sound, etc.). As you can read you don't need to throw an extra notification! When the user clicks on it, the data wil be vailable in the extras of the intent.

c) If your send data only, everything is available in "onMessageReceived" only. So you need to throw a notification on your own.

4. What is"onMessageReceived", "System tray" and "extras of the intent" exactly?

a) onMessageReceived

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)

in the FirebaseMessaging service

b) System tray

Where notifications for the users live

c) extras of the intent

B4X:
Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    Log(in.GetExtra("data"))
End Sub

With

B4X:
Log(in.ExtrasToString)

all "extras" are shown like:

B4X:
Bundle[{google.sent_time=1479841813693, data=8, from=3xxxxxxxx, google.message_id=0:xxxxxxxxxx%yyyyyyyyy, collapse_key=your.package.name}]

I've sent just an "8" as data

in the main activity (= the app will be started "by intent" = Click on the notification)

So now you know how f.e. WhatsApp does it :)

5. Good to know

TO, DATA and NOTIFICATION are keywords! Under this structure you can add maps with any name (key/value) as you like, even 1000 will do (except you don't exceed the max length of 4 KB).
for c) extras of the intent
B4X:
Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    Log(in.GetExtra("data"))
End Sub

How to implement this in B4XPages? Thanks.
 
Top