问题描述
我在开发Android应用程序和服务器的客户端,但现在我发现了GMC-谷歌云端通讯的Android
I developed a client on android app and a server, but now I discovered the GMC- "Google Cloud Messaging for Android"
有什么办法给GCM连接到我的服务器,而无需改变一切?
Is there any way to connect the GCM to my server, without changing everything?
客户端将数据发送到带插座的服务器,我需要的是服务器将数据发送到GCM和GCM将数据发送到所有客户端。
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.
我不需要客户端将数据发送到GCM,所有我需要的是:
I don't need the clients to send data to the GCM, all what I need is :
客户端 - (插座) - >服务器---> GCM --->客户的
client -(socket)--> server ---> GCM ---> clients
有人有任何想法如何做到这一点?
Someone have any idea how to do that?
在此先感谢!
推荐答案
您可以从您的Java服务器发送GCM消息。最简单的方法是使用由谷歌(GCM-的server.jar)提供的服务器库。
You can send GCM messages from your Java Server. The simplest way is to use the server library supplied by Google (gcm-server.jar).
在code发送的消息是简单:
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);
此外,你不得不检查结果,看看你的消息被接受或GCM服务器拒绝。
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.
最后,你必须实现接受来自您的应用程序的注册ID某些Web服务,并在你的数据库保存它。
Finally, you'll have to implement some web service that accepts a registration ID from your app, and saves it in your DB.
这篇关于我的存在服务器连接到GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!