Android Question How to create multiple topics in firebase message

Makumbi

Well-Known Member
Licensed User
How can i create multiple topics to send specific messages to them please help iam just trying out this
because i have different mobile phone numbers registered and i wanted to send out specific messages to them and some times One Message to all of them please help
B4X:
Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    fm.SubscribeToTopic("0772433637") 'you can subscribe to more topics
End Sub

B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    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)
    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=" & 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
End Sub
 

Makumbi

Well-Known Member
Licensed User
B4X:
SendMessage("0772433637", "test", "test")
When i send using
B4X:
fm.SubscribeToTopic("general")
and send using SendMessage("general", "Kabojja Junior School", "Sports Day is Tommorrow please come all!!!!") the message one to only one device but wen i use
B4X:
SendMessage("0772433637", "test", "test")
the message does not come to the other device with this mobile number

This is what is in the firebase messaging service
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    fm.SubscribeToTopic("0772433637") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the message does not come to the other device with this mobile number
but it does come to the device which has this line in

B4X:
 fm.SubscribeToTopic("0772433637") 'you can subscribe to more topics

?

Are you running your app on the device with this phone number? And this device DO register this topic?
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
but it does come to the device which has this line in

B4X:
 fm.SubscribeToTopic("0772433637") 'you can subscribe to more topics

?

Are you running your app on the device with this phone number? And this device DO register this topic?
yes i have installed the app on the device with that number and the number is registered in the app
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Don´t know if it makes any difference:

Try to register a Topic atarting with a character.
Like
B4X:
fm.SubscribeToTopic("No0772433637") 'you can subscribe to more topics

Send a Push to the the Topic "No0772433637" in this case. Worth a try.

I am always using Alphanumeric topics (never starting with numbers).
 
Upvote 0
Top