【问题标题】:google Cloud Messaging Push notification谷歌云消息推送通知
【发布时间】:2015-03-08 14:05:16
【问题描述】:

我可以使用 Google Chrome 上的 POSTMAN 客户端将有效负载消息发送到 GCM 服务器以进行测试吗? 其次如果是,要发送的header和url参数是什么。

【问题讨论】:

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


    【解决方案1】:

    只是为了记录并完成@Alexandru Rosianu 的好答案,GCM 端点不久前发生了变化,建议使用新的端点。以下是取自官方文档的示例:

    认证

    为了发送消息,应用服务器发出一个 POST 请求。例如:

    https://gcm-http.googleapis.com/gcm/send
    

    消息请求由两部分组成:HTTP 标头和 HTTP 正文。

    HTTP 标头必须包含以下标头:

    • Authorization: key=YOUR_API_KEY
    • Content-Typeapplication/json 用于 JSON; application/x-www-form-urlencoded;charset=UTF-8 用于纯文本。如果省略Content-Type,则假定格式为纯文本。

    例如:

    Content-Type:application/json
    Authorization:key=YOUR_API_KEY
    
    {
      "notification": {
          "title": "Portugal vs. Denmark",
          "text": "5 to 1"
      },
      "to" : "bk3RNwTe3H0:CI2k_H..."
    }
    

    HTTP 正文内容取决于您使用的是 JSON 还是纯文本。有关您的 JSON 或纯文本消息可以包含的所有参数的列表,请参阅 the Server Reference

    使用 Curl 的示例:

    # curl --header "Authorization: key=YOUR_API_KEY" \
           --header Content-Type:"application/json" \
           https://gcm-http.googleapis.com/gcm/send \
           -d "{\"notification\": { \"title\": \"Portugal vs. Denmark\"," \
              "\"text\": \"5 to 1\" }, \"to\" : \"bk3RNwTe3H0:CI2k_H...\" }"
    

    【讨论】:

      【解决方案2】:

      是的,您可以使用 POSTMAN。

      此 GCM 通知测试工具通过减少您每次在 POSTMAN 中输入的项目数量,极大地简化了服务器端测试 - http://techzog.com/development/gcm-notification-test-tool-android/

      【讨论】:

        【解决方案3】:

        是的,你可以。

        1。发送带有 JSON 负载的通知

        网址:https://android.googleapis.com/gcm/send

        标题:

        • 授权:key=
        • 内容类型:应用程序/json

        正文(点击“原始”标签):

        {
          "collapse_key": "score_update",
          "time_to_live": 108,
          "delay_while_idle": true,
          "data": {
            "score": "4x8",
            "time": "15:16.2342"
          },
          "registration_ids":["4", "8", "15", "16", "23", "42"]
        }
        

        注意:registration_ids 是唯一必填字段,其他都是可选字段。

        2。发送带有纯文本负载的通知

        网址:https://android.googleapis.com/gcm/send

        标题:

        • 授权:key=
        • 内容类型:application/x-www-form-urlencoded;charset=UTF-8

        正文(点击“x-www-form-urlencoded”标签):

        collapse_key=score_update
        time_to_live=108
        delay_while_idle=1
        data.score=4x8
        data.time=15:16.2342
        registration_id=42
        

        注意:registration_id 是唯一必填字段,其他都是可选字段。


        来源:https://developer.android.com/google/gcm/http.html

        【讨论】:

        • 不错的一个!在我意识到授权标头需要具有值key=ABC... 而不仅仅是ABC... 之前,我只是浪费了 2 个小时试图弄清楚为什么您的答案对我不起作用(401 未经授权的错误),也许这对其他人有帮助-现在很有魅力。
        • 嗨..我使用 json 和表单格式进行了有效负载测试..我收到了推送通知,但我的 event.data 总是为 null ,有什么想法如何传递数据吗?
        • 这里的time_to_live参数只设置为108秒,我相信有点低。
        猜你喜欢
        • 2015-07-17
        • 2014-05-31
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多