Test devices: Huawei P8, Samsung S3 mini, Chuwi P7 and RockChip 10" tablet (Android 4, 5 and 6).
Check that there are no energy saving settings are active (like Huawei has "protected apps" -> you must switch your app to "protected" otherwise it WILL be stopped when the app is in background/screen is off). Maybe Android 7 (or e.g. Samsung has a similar setting). WhatsApp & Co. use the same logic to receive FCM messages. So no magic here.
Datastring can be
anything (lists, maps, etc.). Before sending it, it will converted to a string (always done when you send data via OkHttpUtils job). Please note that FCM messages are limited to 4 KB. Anyway: FCM messages are ment to send short messages to inform the user/app that here's new data to fetch.
Example to send a map (from B4J). You can add whatever you want to the map (I've added date & time here)
Sub SendMessageBTN_Action
Number=Number+1
Dim MyData As Map
MyData.Initialize
MyData.Put("key1","This is the data of key1")
MyData.Put("key2","This is the data of key2")
MyData.Put("key3","This is the data of key3")
MyData.Put("key4","This is the data of key4")
MyData.Put("date",DateTime.Date(DateTime.Now))
MyData.Put("time",DateTime.time(DateTime.Now))
SendMessageToSingleDevice(DevT,MyData)
End Sub
Private Sub SendMessageToSingleDevice(Devtoken As String, MyData As Map)
Dim Job As HttpJob
Job.Initialize("SendMessage", Me)
Dim m As Map = CreateMap("to": $"${Devtoken}"$)
Dim data As Map = CreateMap("data": MyData)
m.Put("data", data)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.Tag=jg
Log(jg.ToString)
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
Note: "to" and "data" are keywords here (Google needs at least these two). Do not change it!
Inside the map named "data" you can put lists, maps, strings, etc.
In
fm_MessageArrived
Sub fm_MessageArrived (Message As RemoteMessage)
Log("Message arrived")
Log($"Message data: ${Message.GetData}"$)
Log("ID: " & Message.MessageId)
Log("From: " & Message.From)
Log("Collapse: " & Message.CollapseKey)
Log(Message.GetData)
Dim MessCompleteMap As Map
MessCompleteMap=Message.GetData
Dim MyData As Map
MyData.Initialize
Dim JSP As JSONParser
JSP.Initialize(MessCompleteMap.get("data"))
MyData=JSP.NextObject
IntentDataMap=MyData
Dim BodyText As String
BodyText="New message: " & CRLF &CRLF
For i=0 To MyData.Size-1
BodyText=BodyText & MyData.GetKeyAt(i) & ": " & MyData.GetValueAt(i) & CRLF &CRLF
Next
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.OnGoingEvent=False
n.Light=True
n.SetInfo2("FCM",BodyText,"This is the tag", "Main")
n.Vibrate=False
n.Notify(1)
End Sub
The activity I use (even here: no additional parameters set!)
#Region Module Attributes
#FullScreen: False
#IncludeTitle: false
#ApplicationLabel: FCM Swipe
#VersionCode: 1
#VersionName: V1.0
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
CallSubDelayed(FirebaseMessaging, "Subscribe")
End Sub
Sub Activity_Resume
Dim in As Intent
in = Activity.GetStartingIntent
If in.HasExtra("Notification_Tag") Then
Log(in.GetExtra("Notification_Tag"))
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
See the docs: There are two types of messages (Notification and data):
https://firebase.google.com/docs/cloud-messaging/concept-options
Notification (I use data messages only: better to handle)
Notification messages
For testing or for marketing and user re-engagement, you can send notification messages using the Firebase console. (please don't)
To programmatically send notification messages using the Admin SDK or the FCM protocols, set the notification key with the necessary predefined set of key-value options for the user-visible part of the notification message. For example, here is a JSON-formatted notification message in an IM app. The user can expect to see a message with the title "Portugal vs. Denmark" and the text "great match!" on the device:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}}
Data messages (My example uses these messages).
Data messages
Set the appropriate key with your custom key-value pairs to send a data payload to the client app. Data messages can have a 4KB maximum payload.
For example, here is a JSON-formatted message in the same IM app as above, where the information is encapsulated in the common data key and the client app is expected to interpret the content:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick":"Mario",
"body":"great match!",
"Room":"PortugalVSDenmark"
}
}}