Android Question FCM 2 problems

MichalK73

Well-Known Member
Licensed User
Longtime User
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:
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
 

KMatle

Expert
Licensed User
Longtime User
The server can send to 2 different channels eg: all, custom

If I get you right: Just subscribe to as many topics as you need to (I do not know what "reg.get("kanal")" contains)

B4X:
fm.SubscribeToTopic("all")
fm.SubscribeToTopic("channel 1")
fm.SubscribeToTopic("peter")
fm.SubscribeToTopic("pizza")

Now the device will get messages of all these 4 topics.

The server can send to 2 different channels

Channel = Topic

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

You just send to a topic and all devices which have subscribed to THIS topic will receive it. Easy.
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
If I get you right: Just subscribe to as many topics as you need to (I do not know what "reg.get("kanal")" contains)

No. I only one subscribe topic. In reg.get("kanal") is write topic for read from FCM.

The server can send the topic "all" or "custom".
I will post the topic in FCM, the app will receive from "all" or "custom" although it will listen only to "custom".
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
Today.

I deleted the old project in the Firebase and I created a new one.
I deleted the 'Objects' folder in the program sources.
I added a new project API key to the server.
Add new 'google-services.json' to app projekt.
I compiled and worked !!!
The application no longer receives all messages from all Topic but those that are subscribed.
The app icon in the notification now comes as a surprise, and as I've written, the default Android icon appears.

So to sum up I do not know what caused the errors. Are some garbage generated in the 'Objects' folder either on the Firebase side?

Topic close.
 
Upvote 0
Top