从azure发送苹果推送通知

时间:2016-01-30 12:31:33

标签: c# azure apple-push-notifications

我正在尝试从azure发送推送通知,当我从我的本地服务器发送其工作正常,但当我上传我的网络应用程序以使其无法正常工作(502 bad gateway error)。所以我的代码在这里:

hostname = "gateway.push.apple.com";
PORT = 2195;
TcpClient client = new TcpClient();

X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(System.Web.HttpContext.Current.Server.MapPath(path)), password);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

client = new TcpClient(hostname, PORT);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);

writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);

writer.Write(HexString2Bytes(ReciverDeviceToken.ToUpper()));
JObject payloadData = new JObject();
JObject aps = new JObject();

aps.Add("alert", Title);
aps.Add("badge", "1");
aps.Add("sound", "sound.caf");
payloadData.Add("aps", aps);

foreach (var m in Messages)
{
    payloadData.Add(m.Key, m.Value);
}

string payloadDataToString = payloadData.ToString();

writer.Write((byte)0);
writer.Write((byte)payloadDataToString.Length);

byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payloadDataToString);

writer.Write(b1);
writer.Flush();

byte[] array = memoryStream.ToArray();

sslStream.Write(array);
sslStream.Flush();
client.Close();

我用Google搜索,但没有结果。有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

试试这个

X509Certificate2 cert = new X509Certificate2(pfxFile, "password", X509KeyStorageFlags.MachineKeySet);

了解更多详情,请参阅此链接

Windows Azure Management Libraries Certification Error on Web Jobs

相关问题