android - 使用 JSON 向 GCM 发送多通知

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

我想使用 GCM 一次向多个注册设备发送多个通知,我知道使用纯文本无法做到这一点,因为它一次只能用于为一个设备发送通知。因此,我决定使用 JSON 制作 HTTP 正文内容,并设法制作以下表示 JSON 对象的字符串:

{ collapse_key : my_CollapseKey, data.message : myMessage,  registration_ids : ["regid1","regid2",...] }

但是每当我发送请求时,我都会收到一个 400 响应,其中指出 Bad Request,当我引用 GCM Architectural Overview 的文档时它指出

Response 400 Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected)

这是我在 ASP.Net 中开发的用于发送请求的网络应用程序的快照:

request.ContentType = "application/json"
        request.Headers.Add("Authorization: key=My_Server_Key")
        request.Headers.Add("Sender: id=myProject_ID")
        Dim collapsKey = Guid.NewGuid.ToString("n")
        Dim data As String = "{ collapse_key : " + collapsKey + "," + " data.message : " + HttpUtility.UrlEncode(TextBox1.Text) + "}" + ", registration_ids : " + jsonids1 + "}"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data)
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

知道如何正确解析我的 JSON 对象并发送多个通知吗? 任何帮助将不胜感激。

问候

最佳答案

JSON 应该是这样的:

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",...],
  "data" : {
    "Team" : "Portugal",
    "Score" : "3",
    "Player" : "Varela",
  },
}

您忘记为数据创建字典。

来自 GCM 文档:

data
A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name. For instance, "data":{"score":"3x1"} would result in an intent extra named score whose value is the string 3x1. There is no limit on the number of key/value pairs, though there is a limit on the total size of the message (4kb). The values could be any JSON object, but we recommend using strings, since the values will be converted to strings in the GCM server anyway. If you want to include objects or other non-string data types (such as integers or booleans), you have to do the conversion to string yourself. Also note that the key cannot be a reserved word (from or any word starting with google.). To complicate things slightly, there are some reserved words (such as collapse_key) that are technically allowed in payload data. However, if the request also contains the word, the value in the request will overwrite the value in the payload data. Hence using words that are defined as field names in this table is not recommended, even in cases where they are technically allowed. Optional.

关于android - 使用 JSON 向 GCM 发送多通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898492/

相关文章:

json - 读取 JSON 架构中的值时遇到意外标记

php - 重复的 Apple APNS 推送通知?

java - 秒表错误,求助!! - java.lang.NumberFormatException : Invalid int: "0:00"

android - 我们如何识别密码类型

javascript - 从http请求函数返回数据

android - 内部错误 : retry receiver class not set yet

ios - OneSignal 按推送通知去 View Controller iOS Swift

java - 将字符串从 Activity 传递到库类

java - 当我使用 StatFs 获取 Android 设备的总内部存储大小时出错

jquery - 将分页 jQuery DataTables 表的所有数据发送到服务器端,而不是仅发送当前页面