0

我在尝试 :

    private void GcmPushNotification(string deviceId)
    {
        string message="New post from Admin";
        string GoogleAppID = "AIzaSyCYesJ5dCK8-O-tf8ZxADELQx5e05P-l5I";
        var SENDER_ID = "854837747831";
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        // --- text
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
            + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse tResponse = tRequest.GetResponse();
        dataStream = tResponse.GetResponseStream();
        StreamReader tReader = new StreamReader(dataStream);
        String sResponseFromServer = tReader.ReadToEnd();
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
    }
4

1 回答 1

0

如果您想向多个设备(最多 1000 个注册 ID)发送相同的通知,则必须使用 JSON 请求格式而不是您当前使用的 URL 编码格式。

例如(取自这里):

Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}
于 2013-08-21T14:13:27.993 回答