0

I am building a chat and I want to send message in FCM to 1000 clients in a group. The message would sent from client to group of 1000 clients (the message not pass through server). I can use the topic method, but if I want to remove a client by the group admin it's not possible. Anyone can propose a solution?

Help me please.

AL.
  • 36,815
  • 10
  • 142
  • 281
Moshe Gil
  • 93
  • 1
  • 8

2 Answers2

1

Topics are indeed public: you (as the developer/administrator of the app) cannot prevent your app's users from subscribing to a topic.

If you want to control the devices that do (and don't) receive your messages, you'll have to send the message to those specific devices. You do this by tracking the instance ID tokens for those devices in a database and then targeting the list of tokens when you send the downstream message.

There is an example of how to manage device tokens and send messages in the Cloud Messaging for Firebase documentation.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If I send 1000 messages to 1000 tokens by use for-loop, I spent alot of resources? the for-loop is inside one thread – Moshe Gil Apr 02 '17 at 14:38
  • 1
    You wouldn't send 1000 messages, you'd send a single message to many ids. See http://stackoverflow.com/questions/40426817/how-to-send-push-notifications-to-multiple-devices-using-firebase-cloud-messagin and http://stackoverflow.com/questions/39375200/fcm-message-to-multiple-registration-ids-limit – Frank van Puffelen Apr 02 '17 at 15:18
0

If you simply want to remove (unsubscribe) some specific members, you can make use of the InstanceID API, specifically batchRemove.

Manage relationship maps for multiple app instances

Using the Instance ID service's batch methods, you can perform batch management of app instances. For example, you can perform bulk addition or removal of app instances to an FCM or GCM topic. To manage app instances, call the Instance ID service at this endpoint, providing the app instance tokens in the JSON body:

https://iid.googleapis.com/iid/v1:batchAdd

https://iid.googleapis.com/iid/v1:batchRemove

Parameters

  • Authorization: key=YOUR_API_KEY. Set this parameter in the header.
  • to : The topic name.
  • registration_tokens : The array of IID tokens for the app instances you want to add or remove.

Results

On success the call returns HTTP status 200. Empty results indicate successful subscription for the token. For failed subscriptions, the result contains one of these error codes:

  • NOT_FOUND — The registration token has been deleted or the app has been uninstalled.
  • INVALID_ARGUMENT — The registration token provided is not valid for the Sender ID.
  • INTERNAL — The backend server failed for unknown reasons. Retry the request.
  • TOO_MANY_TOPICS — Excessive number of topics per app instance.

Example POST request

https://iid.googleapis.com/iid/v1:batchAdd
Content-Type:application/json
Authorization:key=API_KEY
{
   "to": "/topics/movies",
   "registration_tokens": ["nKctODamlM4:CKrh_PC8kIb7O...", "1uoasi24:9jsjwuw...", "798aywu:cba420..."],
}

Example result

HTTP 200 OK
{
  "results":[
    {},
    {"error":"NOT_FOUND"},
    {},
  ]
}
Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • I'm not sure what you're asking. The API works. I've tested it before. – AL. Apr 03 '17 at 06:35
  • As I said, I want to send message from one device to other, without passing through server. I still can use your proposal AL.? – Moshe Gil Apr 03 '17 at 06:46
  • I thought you're concern was "*but if I want to remove a client by the group admin it's not possible*" -- wherein you can make use of the `batchRemove` then sending the message to the topic. If you're aiming for a *serverless* approach, you shouldn't. Sending downstream messages using FCM requires the Server Key, which is strongly advised not to be used on any client side app. – AL. Apr 03 '17 at 07:17
  • I can use the batch proposal on client side? – Moshe Gil Apr 03 '17 at 12:35
  • You can, but you shouldn't. An app server is a *must*. – AL. Apr 04 '17 at 03:44
  • You can propose a android code on the client side use batch option? Please help me AL. – Moshe Gil Apr 04 '17 at 09:21