Android Question Send Firebase Push Notification to unique/single user?

Duck

Member
I have read multiple answers over the internet but they use Topic as the ID or filtering Id on incoming message.
If an app has thousands or even more users I don't think Google Firebase will be happy with so many subscriptions and will see this as mis-use.
Sending to a single topic and then filtering ID in the MessageArrived event seems also not an option because the message will be sent to all so many users first.
So how can i send one single notification?
Lots of games can do that, so we should be able to do so as well.
 

udg

Expert
Licensed User
Longtime User
In fact we can do it. You should collect (and update) the token assigned to each client you want to send private messages to.
B4X:
'Dim m As Map = CreateMap("to": Token)
 Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
Comment/uncomment the proper line for your message sending.
 
Upvote 0

Duck

Member
In fact we can do it. You should collect (and update) the token assigned to each client you want to send private messages to.
B4X:
'Dim m As Map = CreateMap("to": Token)
 Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
Comment/uncomment the proper line for your message sending.
Ok, thank you very much. I found how to obtain the Token.
I guess the token is related to the device itself and not the gmail account of the user?
The latter will be better in case the user installs the app on a new phone or is that not possible?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
AFAIK the token is related to the device.
So, when the app starts you read the token, verify whether it changed from last time and, in case, send it to the "server".
The server records the token (or even the tokens if the user has more devices registered for your service) and it's ready to send out individual messages.

On your DB you could have two tables: one for the customers and one for any number of devices registered by each customer.
This way, you could decide if you want to send a mesage to a persone (sending to all his/her devices) or to a specific device.

Anotehr point. Don't rely 100% on the broker sending messages. In general, FCM successfully sends messages even to devices turned off (for some time, I guess).
But I find it wiser to query the "server" about new messages on app start (this means you locally to the device records the ID of the last one received).
Obviously all that depends on the kind of app you're developing.
 
Upvote 0

Duck

Member
AFAIK the token is related to the device.
So, when the app starts you read the token, verify whether it changed from last time and, in case, send it to the "server".
The server records the token (or even the tokens if the user has more devices registered for your service) and it's ready to send out individual messages.

On your DB you could have two tables: one for the customers and one for any number of devices registered by each customer.
This way, you could decide if you want to send a mesage to a persone (sending to all his/her devices) or to a specific device.

Anotehr point. Don't rely 100% on the broker sending messages. In general, FCM successfully sends messages even to devices turned off (for some time, I guess).
But I find it wiser to query the "server" about new messages on app start (this means you locally to the device records the ID of the last one received).
Obviously all that depends on the kind of app you're developing.
Thanks, that was very helpfull
 
Upvote 0
Top