1

我正在使用 asp.net 向 android 发送 gcm 推送通知,但消息无法相互发送,但如果我关闭平板电脑的 wi-fi 并再次打开,那么两台平板电脑都会收到所有消息。

波纹管是 GCM 代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;


public class AndroidGCMPushNotification
{

    public string SendNotification(string deviceId, string message)
    {
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";

        // --- text
        //tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        //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 + "";

        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        string postData = "delay_while_idle=0&data.message="
            + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";



        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleApiKey));

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



    public string GoogleApiKey
    {
        get { return m_strGoogleAppID; }
        set { m_strGoogleAppID = value; }
    }


    public string SenderId
    {
        get { return m_strSenderId; }
        set { m_strSenderId = value; }
    }


    string m_strSenderId;
    string m_strGoogleAppID;
}
4

0 回答 0