简体   繁体   中英

Sending an upstream topic message from Android to GCM

How can I send a GCM mesage from inside my Android application such that every other device that is registered to a certain topic receive that message?

In the documentation it says this is the way of sending an upstream GCM message:

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String GCM_SENDER_ID = "715097164599";
    AtomicInteger msgId = new AtomicInteger();
    String id = Integer.toString(msgId.incrementAndGet());
    Bundle data = new Bundle();

    data.putString("message", "This is a GCM Topic Message!");

    try
    {
        gcm.send(GCM_SENDER_ID + "@gcm.googleapis.com", id, data);
    }
    catch(IOException e)
    {
        throw new RuntimeException(e);
    }

But notice there is no place there to specify the topic this message should go to.

I thought of adding:

data.putString("topic", "/topics/global");

But that puts the "to" field inside of the message payload, which is not what the GCM server expects.

How can I achieve sending a message to all subscribers without using the HTTP interface of GCM ?

Thanks.

It looks like this functionality is not available (upstream from device to topic group).

One workaround (see edit) you can do is to send an actual upstream message to your server and letting the server know of your intentions. Of course, you will need to add this logic to your server code. In the process, you could add a value in data to signify the message's intent to be broadcast downstream to a certain topic.

Edit: Refer to @TheWonderBird comment below for potential security risk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM