1

这是编写 CCS 服务器的 Google 文档中的 python 代码:

https://developer.android.com/google/gcm/gs.html#server

我想出了其中的大部分,以及如何使用https://github.com/astro/node-xmpp在 Javascript 中对其进行编码

但我无法理解如何使用模板发送数据,正是这部分代码:

def send(json_dict):
  template = ("<message><gcm xmlns='google:mobile:data'>{1}</gcm></message>")
  client.send(xmpp.protocol.Message(
      node=template.format(client.Bind.bound[0], json.dumps(json_dict))))

在 node-xmpp 中,发送是通过以下方式完成的:

var cl = new xmpp.Client({ jid: username,
                           password: password });
cl.addListener('online',
               function() {
                   argv.slice(5).forEach(
                       function(to) {
                           cl.send(new xmpp.Element('message',
                                                    { to: to,
                                                      type: 'chat'}).
                                   c('body').
                                   t(argv[4]));
                       });

我了解正在发送的 JSON,但我无法绑定他们在 Python 中管理的模板。有什么帮助吗?

4

1 回答 1

1

重要的部分是以所需的格式发送消息:

<message id="">
  <gcm xmlns="google:mobile:data">
  {
      "to":"REGISTRATION_ID",  // "to" replaces "registration_ids"
      "message_id":"m-1366082849205" // new required field
      "data":
      {
          "hello":"world",
      }
      "time_to_live":"600",
      "delay_while_idle": true/false
  }
  </gcm>
</message>

是否使用模板并不重要。我不知道python也不知道javascript,但是python示例中模板的目的似乎只是为了避免每次发送消息时都需要编写包装JSON的xml标签。您可以在发送消息时将它们附加到 JSON。

于 2013-06-19T14:34:32.617 回答