简体   繁体   English

使用C#为Android推送通知?

[英]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. 我正在为Android Mobile创建推送通知,我的代码是这样的,但是抛出Exception吗?对我来说,这对我来说非常重要吗?谢谢。

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) 在这里,您有我的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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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