Windows azure 中的 APNS

Posted

技术标签:

【中文标题】Windows azure 中的 APNS【英文标题】:APNS in windows azure 【发布时间】:2013-05-16 13:55:51 【问题描述】:

我一直在从 Windows azure 实现苹果推送通知。我已经能够连接到 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.");

    

【问题讨论】:

【参考方案1】:

您使用的是简单的二进制格式,它不会返回错误响应。

您应该切换到增强型二进制格式,在该格式中您发送(除了简单 API 发送的内容之外)消息 ID 和到期时间,并且可以从套接字读取错误响应。

Apple Push Notifications Guide 是 recently updated,现在他们甚至没有提到简单的格式,所以可能不再支持了。

【讨论】:

感谢您指导我阅读这篇文章,但是您有什么方法可以分享一些 C# 中的示例代码吗?因为我仍然不确定如何处理 APNS 服务器可能发送的错误响应。因为到目前为止,即使我发送的二进制格式错误,它也不会发送任何错误响应。 旧格式从不发送错误消息。至于C#示例代码,试试Push Sharp。 Push Sharp 似乎是一个方便的库。知道我是否可以将其用于商业应用程序?比如它是麻省理工学院的许可证吗?它被称为“Apache PushSharp Copyright 2012 The Apache Software Foundation”,这是否意味着我不能用于商业目的? 我不知道。我自己没有使用它(我的服务器是用 Java 编写的)。

以上是关于Windows azure 中的 APNS的主要内容,如果未能解决你的问题,请参考以下文章