如何通过GCM在C#.Net上发送Android推送通知

前端之家收集整理的这篇文章主要介绍了如何通过GCM在C#.Net上发送Android推送通知前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是所有 Android的GCM推送通知的新手,我已经阅读了堆栈文章,但无法得到一个直接的答案.我也读了 Create push notification in android,以更好地了解GCM如何工作.我也使用了SDK提供的gcm-demo-server和gcm-demo-client.但是,这是我的怀疑和迄今为止所尝试的:

>关于我的链接,具有应用程序的手机注册获取注册密钥.这是使用相同应用程式的所有手机的唯一键?
>这个注册密钥在任何情况下是否到期? (例如在背景上运行的应用程序)
>假设我有注册码,我已经尝试了以下代码片段,通过GCM将通知推送到我的应用程序.这是写在c#.net上.请让我知道上面提到的是否可以使用以下代码片段来实现:

private string SendGCMNotification(string apiKey,string postData,string postDataContentType = "application/json")
    {
        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

        // MESSAGE CONTENT
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // CREATE REQUEST
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = postDataContentType;
        Request.Headers.Add(string.Format("Authorization: key={0}",apiKey));
        Request.ContentLength = byteArray.Length;

        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray,byteArray.Length);
        dataStream.Close();

        // SEND MESSAGE
        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text = "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();

            return responseLine;
        }
        catch (Exception e)
        {
        }
        return "error";
    }

>有没有手机首先在我们的定制服务器中注册的方式发送推送通知

解决方法

参考代码
public class AndroidGCMPushNotification
{
    public AndroidGCMPushNotification()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public string SendNotification(string deviceId,string message)
    {
        string SERVER_API_KEY = "server api key";        
        var SENDER_ID = "application number";
        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}",SERVER_API_KEY));

        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() + "&registration_id=" + deviceId + "";
        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray,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;
    }
}

推荐链接

http://www.codeproject.com/Tips/434338/Android-GCM-Push-Notification

猜你在找的C#相关文章