如何通过GCM向多个用户传递通知?

时间:2013-08-21 12:28:51

标签: google-cloud-messaging

我在尝试:

    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();
    }

1 个答案:

答案 0 :(得分:0)

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

例如(取自here):

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

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}