Connect my exist server to GCM
Asked Answered
C

1

0

I developed a client on android app and a server, but now I discovered the GMC- "Google Cloud Messaging for Android"

Is there any way to connect the GCM to my server, without changing everything?

The client send data to the server with socket, and what I need is that the server will send the data to the GCM, and the GCM will send the data to all the clients.

I don't need the clients to send data to the GCM, all what I need is :

client -(socket)--> server ---> GCM ---> clients

Someone have any idea how to do that?

Thanks in advance!

Camisado answered 23/7, 2014 at 19:5 Comment(0)
K
1

You can send GCM messages from your Java Server. The simplest way is to use the server library supplied by Google (gcm-server.jar).

The code for sending a message is as simple as :

Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
    .delayWhileIdle(true)
    .addData("key1", "value1")
    .addData("key2", "value2")
    .build();
Result result = sender.send(message, registrationId, numOfRetries);

In addition, you'd have to check the result, to see if your message was accepted or rejected by GCM server.

This example shows how to send a message to a single device. There's a similar way to send the same message to multiple devices.

Finally, you'll have to implement some web service that accepts a registration ID from your app, and saves it in your DB.

Kay answered 24/7, 2014 at 1:14 Comment(4)
Wow. That's defenetly more friendly! Thx!! Is there a way to send the message to all devices?? and is there a simple way like this to receive the message from the device?Camisado
@Camisado Sending the message to all devices requires specifying the registration IDs of all the devices. You can't just tell GCM to send a message to all the registered devices. The receiving of the messages is demonstrated in the official demo app.Kay
So how I get the registration IDs of all the devices?Camisado
@Camisado The demo shows you how to register the app to GCM and get a registration ID. After you get the registration ID, you send it to your server (you'll need to implement some service that accepts a registration ID and stores it in your DB).Kay

© 2022 - 2024 — McMap. All rights reserved.