Reputation: 75
I want to be able to call a function like this: (from client)
void sendNotificationToGroupMembers(String groupId, String message)
with a DB Structure like This
/userTokens/userId:userInstanceId
/groupMembers/groupId/[userIds...]
/serverRequests/...
Now, I after research I think I have an idea on how to approach it, but I don't know if it's the right approach and it is very involved for something I consider should be a very simple task, so I was wondering if there is an easier/simpler way to approach my problem.
This is how I would do it
[Backend]
/serverRequets/
inside a Servlet running on a Google Cloud Flexible[Client]
/serverRequests/push/groupId:message
[Backend]
/groupMembers/groupId/
and save userIds in a list/userTokens/userId
and save to a listBut I see a problem at steps 4. Because to get the userInstanceId I must make a request with ValueEventListener for each userId, but since they are asynchronous, how do I determine when all the instanceId's have been downloaded? Do I set a timer of 5 seconds? Do I check how large the userId list is, and then use something like LinkedBlockingQueue to publish to a Thread each time an instanceId is downloaded, and only if it reaches the size, do the step 5?
Because I don't find a multiple .equalTo
query, and I can't download the entire userTokens node...
Upvotes: 2
Views: 778
Reputation: 8020
First off, I would use Cloud Functions for this, not a custom listener you set up. You can see an example of a notification function here
Next, I would consider using topic messaging, since it would cover the use case you are describing (sending a message to members of a group). I employed this myself and it works a charm.
You flow would now be like this:
Client tells FCM it would like to subscribe to topic groupID
.
At some point there is a message pushed to an endpoint in your /groupId/messages/
(or however you want to structure it, remember, denormalization of a NoSQL database is good!)
Then have a cloud function listen to /{groupId}/messages/{messageId}
and post a new message to your topic /groupId
I hope this makes sense and I understood your question correctly
Upvotes: 2