Jump to content
Alin

FCM with Kastri - how to send multiple messages (multicast)

Recommended Posts

First I want to .honestly. congratulate and thank Dave Nottage and all his collaborators for the great work which is the Kastri Library. I would praise it more but I don't want to annoy the other readers.

 

And now for the question itself 🙂

 

I have succeded in programatically using Kastri and PushIt example parts to send a single message (a.k.a. notification) through Firebase Cloud Messaging System, to a single recipient, using a single token.

 

I have seen that Google's SDK can multicast a message to up to 500 recipients - for example if I want to send the same message simultaneously to many users who have nothing in common, thus without relying on topics, groups and so on, which are not appropriate for my use case scenario.

I want to somehow feed FCM a list of 500 tokens so it could send the message to all of them.

 

How can this be done in Delphi? How should one implement this?

 

Is it a different request / JSON syntax?

 

Or is it done by simply sending 500 separate messages, each to a single recipient? And if it can only be done using a succession of single-recipient messages, how many FCMSenders can I set up to use on a single FCM service account? I need short quick bursts from time to time.


The example I want to expand to multiple tokens/recipients is something like this (below is the JSON content from the memo of the PushIt example, I deleted some CRLFs):

 

{"message":{
    "token":"c1bF8qRZfB2i_9FgWtLsX4DVWvDMMb6EOHKAfNdOxt-LubU9by-akksT5uVEKzuGpKJrSrGmOOK0ypBrBc9m.....",
    "notification":{
      "title":"Title1",
      "body":"Body1"
    },
    "apns":{
      "payload":{
        "aps":{
          "badge":0,
          "mutable-content":1
  }   }   }  }  }

 

I apologize if this question has already been asked, I wasn't able to find the thread.

 

Thank you again!

Alin

Share this post


Link to post
6 hours ago, Alin said:

First I want to .honestly. congratulate and thank Dave Nottage and all his collaborators for the great work which is the Kastri Library

Thanks!

6 hours ago, Alin said:

I want to somehow feed FCM a list of 500 tokens so it could send the message to all of them.

It's much easier to have each of the devices subscribe to a topic using the SubscribeToTopic method, and simply send the push notification using a topic, rather than multiple tokens.

6 hours ago, Alin said:

How should one implement this?

If you really do want to base it on individual tokens, see:
 

  https://firebase.google.com/docs/cloud-messaging/js/device-group


It's in Javascript, however something similar could be constructed in Delphi.

  • Like 1

Share this post


Link to post

Thank you very much for answering!

 

I will try to give some more details about my issue.

 

I have around 100,000 (not paying) users in this app and I need to be able to send notifications to some of them at any moment, for example 1,500 now and 2,000 in an hour. These two (1.5k and 2k) recipient sets are completely random, they have nothing in common except for a coincindence when 4 users may be the same in both sets; they are not subscribed to anything and I must be able to pick any number of random users and send a notification to each of them.

 

I could create groups or topics but those groups/topics would mean nothing after the notification messages are sent, as the next batch could be just as random as the previous one. Grouping them in any consistent way is impossible and any group or topic should dissolve after I sent a certain batch to that group, since the next set will not be based on any former group. And the most difficult part is that I don't get to pick those recipients - but a special kind of app users do, and they have no fixed criteria for this (they are administrators of certain things and I have to let them do their thing; the users sometimes wait for those notifications and even worse, sometimes they need them in case of some emergency; this is not an app for fun).

 

I wish to do it like sending a newsletter for example: if I own/control the database then I can pick at any given time 2,500 random recipients and send the e-mail or the snail-mail to them if necessary, depending on criteria that can be anything. But those administrators above decide, and they don't group users into groups or topics, it's about very different criteria. How can I do the same for these FCM notifications?

 

What is the best solution in this case, what would you recommend me to do?

 

My fall-back-to scenario is the brute force approach, where I would create a few servers to send these messages one by one, through a few FCM service accounts. This is quite blunt and I hope there is a more refined possibility. I was hoping for some "group message" constructed in JSON, with more than one recipient in the token list.

 

Or could I create a topic and subscribe the users to it from the server side, then send the message to that topic, and then dissolve it?

 

Thank you,

Alin

Edited by Alin
Rephrase

Share this post


Link to post
10 hours ago, Alin said:

I can pick at any given time 2,500 random recipients and send the e-mail or the snail-mail to them if necessary, depending on criteria that can be anything. But those administrators above decide, and they don't group users into groups or topics, it's about very different criteria. How can I do the same for these FCM notifications?

In this instance topics won't help, since the topic needs to be subscribed to from the client side.

10 hours ago, Alin said:

Or could I create a topic and subscribe the users to it from the server side, then send the message to that topic, and then dissolve it?

As above, topics are subscribed to from the client side, so for your case you would need to implement message groups, as per the link I posted in my previous reply. As long as you have the tokens, you can select some at random, send a request to FCM for a notification key that includes all the selected tokens, then send a message using that key.

Share this post


Link to post

Thank you very much for replying!

 

I now understand the matter with the topics, thank you!

 

I have studied the groups (via the link you posted) and I think I have found that:

 

1. they have at most 20 members (perhaps they are only targeted for groups of devices belonging to the same user / person)

" The maximum number of members allowed for a notification key is 20. "

 

2. they are accessible only through the old (legacy) HTTP protocol, which is severely discouraged or even disabled for new projects

" You can use device group messaging via the legacy XMPP or HTTP protocols on your app server. "

 

In this case: creating a group, sending the message and then deleting the group could be only marginally more effective than the brute force approach of sending single-device messages.

 

Did I understand this correctly?

 

Have a nice day!

Alin

Share this post


Link to post
1 hour ago, Alin said:

1. they have at most 20 members (perhaps they are only targeted for groups of devices belonging to the same user / person)

" The maximum number of members allowed for a notification key is 20. "

 

2. they are accessible only through the old (legacy) HTTP protocol, which is severely discouraged or even disabled for new projects

" You can use device group messaging via the legacy XMPP or HTTP protocols on your app server. "

I missed that part, thanks. I was focusing more on how it might be implemented.

1 hour ago, Alin said:

Did I understand this correctly?

It seems that way.

On 5/5/2023 at 9:06 PM, Alin said:

I need to be able to send notifications to some of them at any moment, for example 1,500 now and 2,000 in an hour

Given that, you'd need to code your server to be able to handle sending a message every second or so. Since sending the message seems to sometimes take more than a second, you'd need to spawn a thread or two (or three) to do the work.

Edited by Dave Nottage

Share this post


Link to post

Thank you!

 

So I will have to resort to the brute force approach.

 

In the future I think I will have to handle at least 50 messages / second with peaks of 100 up to 200 messages / second (sometimes they are time-critical), which I am thinking to send using multiple threads and possibly multiple FireBase service accounts.

 

I think I will almost need a separate machine to do all this - if the project expands.

 

I don't know which limits are set by Google for those who send messages through this method, I may have some (unpleasant) surprises in the future.

 

Thank you again for all your help and your libraries!

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×