问题
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!
回答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.
来源:https://stackoverflow.com/questions/24918842/connect-my-exist-server-to-gcm