简体   繁体   English

Google Cloud Messaging-发送消息

[英]Google Cloud Messaging - Send Message

I have cloud database so I have all device's reg ID's. 我有云数据库,所以我有所有设备的注册ID。

Do I have to use a server for sending message ? 我必须使用服务器发送消息吗? Can I send message directly from one Android device to another just using regID ? 我可以仅使用regID将消息直接从一台Android设备发送到另一台吗? Can I use Android device as server ? 我可以使用Android设备作为服务器吗?

If each Android device has access to the database of registration IDs, it can send GCM messages to other devices without using a server. 如果每个Android设备都可以访问注册ID数据库,则可以在不使用服务器的情况下将GCM消息发送到其他设备。 You simply have to execute the same HTTP request to GCM that you would normally do from a server. 您只需执行通常从服务器执行的对GCM的HTTP请求即可。 That request would go directly to GCM server and from there would be delivered to your app on another device. 该请求将直接发送到GCM服务器,并从那里发送到另一台设备上的您的应用。 To execute that request you need your app to know both the sender ID (for registering to GCM) and the API key (for sending messages) in addition to the recipient registration ID. 要执行该请求,除了接收者注册ID外,您的应用还需要知道发送者ID(用于注册到GCM)和API密钥(用于发送消息)。

You can't use the gcm.send method. 您不能使用gcm.send方法。 That method sends messages to your server. 该方法将消息发送到您的服务器。

Yes you can send a message to your server: 是的,您可以向服务器发送消息:

data.putString("my_message", "Hello World");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);

You can's send a message directly to other device because only Google's servers know where the receiver device is. 您可以直接将消息发送到其他设备,因为只有Google的服务器知道接收方设备的位置。 The scheme of messaging looks like this: 消息传递方案如下所示:

Sender > Google's server > Your server > Google's server > Receiver

Theoretically you can add receiver's id to the sending data, get it on your server and then send new message to the concrete device. 从理论上讲,您可以将接收者的ID添加到发送数据中,在服务器上获取它,然后将新消息发送到具体设备。

You can find more detailed description in the documentation 您可以在文档中找到更详细的描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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