简体   繁体   中英

Push notifiaction for the Android using the C#?

I am Working on the Creating the Push Notification for the Android Mobile my code is like this but throwing the Exception?Help me it is very important for me?thanks in advance.

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

Here you have my code for server side in python (it works)

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM