c# - Windows Azure 中的 APNS

标签 c# .net azure push-notification apple-push-notifications

我一直在从 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.");

    }

最佳答案

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

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

Apple 推送通知指南是 recently updated ,现在他们甚至没有提到简单格式,所以也许不再支持它。

关于c# - Windows Azure 中的 APNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589403/

相关文章:

php - PDO 异常 - SQLSTATE[HY000] [2002] 尝试以 Azure 中的访问权限禁止的方式访问套接字

c# - 获取变量的地址位置

c# - 仅包含接口(interface)的 WinRT 组件 DLL?

c# - 使用 StreamWriter 后文件为空

c# - 如何在我的应用程序 C# 中创建控件的快捷方式列表?

找不到Asp.net Core 2.0静态文件错误

azure - Azure 有多少个核心?

azure - 无法在azure中选择订阅ID

c# - 反向多对多 Dictionary<key, List<value>>

c# - 如何为所有子控件启用拖放