Hello.
I already use FCM in another application and everything I copied from the old one and I have 2 strange problems.
1. The server can send to 2 different channels eg: all, custom. I do not know what the B4A application's theme is for, and not the one it's assigned to. "Fm.SubscribeToTopic (reg.get (" channel "))"
2. While receiving a message, I do not substitute the original "n.Icon =" icon "" for the original "B4A" icon.
Now I can use the 'send' function to add 'topic' to 'data' and then check if it matches the program with the given communication channel. But that's not it! The application should listen to the set channel and receive all for the application.
Server code:
App code.
I already use FCM in another application and everything I copied from the old one and I have 2 strange problems.
1. The server can send to 2 different channels eg: all, custom. I do not know what the B4A application's theme is for, and not the one it's assigned to. "Fm.SubscribeToTopic (reg.get (" channel "))"
2. While receiving a message, I do not substitute the original "n.Icon =" icon "" for the original "B4A" icon.
Now I can use the 'send' function to add 'topic' to 'data' and then check if it matches the program with the given communication channel. But that's not it! The application should listen to the set channel and receive all for the application.
Server code:
B4X:
public Sub SendMessage(Topic As String, Title As String, Body As String, id As String)
'StartMessageLoop
Dim Job As HttpJob
Job.Initialize("fcm", Me)
Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
Dim data As Map = CreateMap("title": Title, "body": Body, "id": id, "topic": Topic)
m.Put("data": data)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
Job.GetRequest.SetContentType("application/json")
Job.GetRequest.SetHeader("Authorization", "key=" & Main.API_KEY)
End Sub
Sub JobDone(job As HttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
job.Release
StopMessageLoop '<-- non ui app only
StartMessageLoop
End Sub
App code.
B4X:
Sub Process_Globals
Public fm As FirebaseMessaging
Private FileCrypt As RandomAccessFile
Private registry As List
Private reg As Map
'Private ph As PhoneId
End Sub
Sub Service_Create
fm.Initialize("fm")
If File.Exists(File.DirInternal, "config") Then
FileCrypt.Initialize(File.DirInternal, "config", True)
reg = FileCrypt.ReadEncryptedObject(Main.has, 0)
fm.SubscribeToTopic(reg.get("kanal"))
End If
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
End Sub
Sub fm_MessageArrived (message As RemoteMessage)
Log("Message arrived")
Log($"Message data: ${message.GetData}"$)
If reg.Get("kanal") = message.GetData.Get("topic") Then
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.AutoCancel = False
Log("-->"&message.GetData.Get("id"))
viewMessage.id = message.GetData.Get("id")
n.SetInfo(message.GetData.Get("title"), message.GetData.Get("body"), viewMessage)
n.Notify(1)
End If
End Sub