【问题标题】:Can a GCM client (device) send message to another GCM client directly without going to serverGCM客户端(设备)是否可以直接向另一个GCM客户端发送消息而不去服务器
【发布时间】:2014-04-02 03:29:24
【问题描述】:

根据

的文档

http://developer.android.com/google/gcm/client.html

http://developer.android.com/google/gcm/server.html

您必须实现的第 3 方应用服务器。此应用程序服务器通过所选的 GCM 连接服务器将数据发送到支持 GCM 的 Android 应用程序。

从上面看,这意味着您不能将消息从客户端直接发送到另一个客户端。

请有人确认。

【问题讨论】:

    标签: 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 :(

    你好,马耳他

    【讨论】:

    • 使用此代码时出现身份验证错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2015-01-05
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多