【问题标题】:Can a GCM client (device) send message to another GCM client directly without going to serverGCM客户端(设备)是否可以直接向另一个GCM客户端发送消息而不去服务器
【发布时间】:2014-04-02 03:29:24
【问题描述】:
【问题讨论】:
标签:
registration
google-cloud-messaging
【解决方案1】:
可以将 GCM 消息直接从您的设备发送到 Google 服务器,而无需任何 3rd-party-server。
只需像这个例子一样运行一个 AsyncTask:
HttpPost httppost;
try
{
httppost = new HttpPost("https://android.googleapis.com/gcm/send");
}
catch (IllegalArgumentException e)
{
return false;
}
httppost.setHeader("Authorization","key=xxxxxxxxxxxxxx");
httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
postParams.add(new BasicNameValuePair("registration_id", "xxxxxxxxxxxxx"));
postParams.add(new BasicNameValuePair("data.mode", "exampleContentOfYourMessage"));
HttpResponse response;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity entity;
try {
httppost.setEntity(new UrlEncodedFormEntity(postParams, "utf-8"));
}
catch (UnsupportedEncodingException e)
{
return false;
}
try {
response = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
entity = response.getEntity();
if (entity != null && response.getStatusLine().getStatusCode() == 200)
{
return true;
}
else
{
Log.e("Tag","No Connection");
return false;
}
但我不知道如何将 GCM 消息发送到多个注册 ID :(
你好,马耳他