[英]APNS in windows azure
我一直在执行来自Windows Azure的Apple Push通知。 我已经能够连接到APNS服务器并使用证书进行身份验证。 当我将流写入服务器时,我没有任何异常。 但是出于奇怪的原因,设备没有收到通知。 该应用程序已注册用于推送通知。 我不确定是什么问题。 有什么方法可以检查我发送到APNS服务器的通知是否有效,或者即使APNS服务器已将通知发送给应用程序? 下面是我的代码。
如果有经过测试且可以正常工作的代码比这更好的实现,我也将不胜感激
APPLEHOST = "gateway.sandbox.push.apple.com";
APPLEPORT = 2195;
private void InitializeAPN()
{
applePushNotificationClient = new TcpClient(APPLEHOST, APPLEPORT);
sslStream = new SslStream(applePushNotificationClient.GetStream(), false);
try
{
sslStream.AuthenticateAsClient(APPLEHOST, APPLE_CLIENT_CERT_COLLECTION, SslProtocols.Tls, false);
}
catch (AuthenticationException ex)
{
Trace.WriteLine("Could not open APN connection: " + ex.ToString());
}
Trace.WriteLine("APN connection opened successfully.");
}
public void SendAPNMessage(string message, string deviceID)
{
try
{
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
// construct the message
binaryWriter.Write((byte)0);
binaryWriter.Write((byte)0);
binaryWriter.Write((byte)32);
// convert to hex and write
byte[] deviceToken = new byte[deviceID.Length / 2];
for (int i = 0; i < deviceToken.Length; i++)
{
deviceToken[i] = byte.Parse(deviceID.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
binaryWriter.Write(deviceToken);
// construct payload within JSON message framework
var json = new JArray(new JObject(new JProperty("aps", new JObject(new JProperty("alert", message), new JProperty("badge", 1))))).ToString();
byte[] payloadBytes = System.Text.Encoding.UTF8.GetBytes(json);
// write payload data
binaryWriter.Write((byte)0);
binaryWriter.Write((byte)payloadBytes.Length);
binaryWriter.Write(payloadBytes);
binaryWriter.Flush();
// send across the wire
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
Trace.WriteLine("Message successfully sent.");
}
您正在使用简单的二进制格式,该格式不返回错误响应。
您应该切换到增强型二进制格式,在该格式中,您发送(除了简单API发送的内容之外)消息ID和到期时间,并可以从套接字读取错误响应。
《 Apple Push Notifications Guide》 最近更新了 ,现在他们甚至没有提到简单的格式,因此也许不再受支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.