1

我正在为 Android Mobile 创建推送通知,我的代码是这样的,但抛出异常?帮帮我,这对我来说很重要?提前谢谢。

public string SendNotificationAndoride(string deviceId, string message)
        {
            string GoogleAppID = "google application id";        
            var SENDER_ID = "9999999999";
            var value = message;
            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
            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();
            return sResponseFromServer;
    }
4

1 回答 1

0

在这里,您有我在 python 中的服务器端代码(它有效)

def sendAndroidPushNotification(registration_id, collapse_key, data) :
    try:
        auth = "AIzsS3DPP3GYG38uw12qYQpJwGZ9AtliAsrJnhY"
        push_request = urllib2.Request("https://android.googleapis.com/gcm/send")

        jsondata = None
        if collapse_key :
            jsondata = simplejson.dumps({'registration_ids': [registration_id,],
                                         'collapse_key'    : collapse_key,
                                         'data'            : data,
                                         })
        else :
            jsondata = simplejson.dumps({'registration_ids': [registration_id,],
                                         'data'            : data,
                                         })

        push_request.add_data(jsondata)

        push_request.add_header('Authorization', 'key=' + auth)
        push_request.add_header('Content-Type', 'application/json')
        push_request.add_header('Content-Length', len(jsondata))

        result = urllib2.build_opener().open(push_request)
        parsed = simplejson.load(result)
#        print('Success {0}, Failures {1}, Canonical IDs: {2}'.format(parsed['success'],
#                                                                     parsed['failure'],
#                                                                     parsed['canonical_ids']))
#        print 'result: ' + str(parsed['results'][0])
于 2012-11-28T12:29:40.110 回答