1

私は Android モバイル用のプッシュ通知の作成に取り組んでいます。私のコードはこのようなものですが、例外をスローしていますか?それは私にとって非常に重要です?事前に感謝します。

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 に答える