【问题标题】:GoogleCloudMessaging AndroidGoogleCloudMessaging Android
【发布时间】:2013-08-02 10:31:57
【问题描述】:

我在使用新的GoogleCloudMessaging API 实现上游消息时感到困惑:

public void onClick(final View view) {
    if (view == findViewById(R.id.send)) {
        new AsyncTask() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    Bundle data = new Bundle();
                    data.putString("hello", "World");
                    String id = Integer.toString(msgId.incrementAndGet());
                    gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
                    msg = "Sent message";
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            }
        }.execute(null, null, null);
    } else if (view == findViewById(R.id.clear)) {
        mDisplay.setText("");
    } 
}

我们使用 SENDER_ID id 将消息 (XMPP) 发送到 GCM 服务器,那么我的第三方服务器如何仅使用 SENDER_ID 识别我的设备?

gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);

【问题讨论】:

    标签: android push-notification google-cloud-messaging


    【解决方案1】:

    您发布的代码将消息从您的设备发送到您的第三台服务器,而不是另一台设备。您的服务器应该与 Cloud Connection Server 建立连接,并且由于建立该连接需要使用 SENDER_ID + "@gcm.googleapis.com" 作为用户名,因此 GCM 服务器可以识别您的第 3 方服务器。

    当您的第 3 方服务器向您的设备发送消息时,它会使用注册 ID,该 ID 在特定设备上识别您的应用。

    编辑:

    我可能误解了你的问题。如果您询问第 3 方服务器如何知道哪个设备向其发送了上游消息,您的第 3 方服务器收到的消息包含发件人的注册 ID,如您在此处看到的:

    <message id="">
      <gcm xmlns="google:mobile:data">
      {
          "category":"com.example.yourapp", // to know which app sent it
          "data":
          {
              "hello":"world",
          },
          "message_id":"m-123",
          "from":"REGID"
      }
      </gcm>
    </message>
    

    即使您在调用 gcm.send(...) 时没有指定注册 ID,GCM 也知道是哪个设备发送了消息,因此(假设您的应用注册到 GCM 并因此具有注册 ID)他们可以将注册 ID 添加到消息(我不确定他们是否在连接 GCM 服务器之前将其添加到客户端,或者是否将其添加到 GCM 服务器,但这并不重要)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      相关资源
      最近更新 更多