在C#苹果推送通知服务端 [英] Apple push notification server side in C#

查看:183
本文介绍了在C#苹果推送通知服务端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.net和做APN服务器端的C#。我使用的是下面code到邮件推送到苹果服务器。

I'm new to .net and doing apn server side in C#. I'm using below code to push the messages to apple server.

    private void pushMessage()
    {
        int port = 2195;
        String deviceID = "4564c705 63b371aa 3811699e 1e4ac3d2 ba592b27 f2a5a613 d25cd035 xx213e54";
        String hostname = "gateway.sandbox.push.apple.com";     // TEST
        //String hostname = "gateway.push.apple.com";           // REAL

        //        @"cert.p12";
        String certificatePath = HttpContext.Current.Server.MapPath("Certi.p12");
        //X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, "");

        X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

        X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
        TcpClient client = new TcpClient(hostname, port);

        // _apnsStream = new SslStream(_apnsClient.GetStream(), false, validateServerCertificate, SelectLocalCertificate);
        //SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate),null);

        SslStream sslStream = new SslStream(client.GetStream(), false, validateServerCertificate, SelectLocalCertificate);
        try
        {
            sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
        }
        catch (Exception e)
        {
            throw (e);
            client.Close();
            return;
        }

        MemoryStream memoryStream = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memoryStream);
        writer.Write((byte)0);  //The command
        writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
        writer.Write((byte)32); //The deviceId length (big-endian second byte)

        writer.Write(HexStringToByteArray(deviceID.ToUpper()));
        String payload = "{\"aps\":{\"alert\":\"hello\",\"badge\":0,\"sound\":\"default\"}}";
        writer.Write((byte)0);
        writer.Write((byte)payload.Length);
        byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
        writer.Write(b1);
        writer.Flush();
        byte[] array = memoryStream.ToArray();
        sslStream.Write(array);
        sslStream.Flush();
        client.Close();
    }

    private string HexStringToByteArray(string p)
    {
        throw new NotImplementedException();
    }

在执行时,得到错误,如A调用SSPI失败,请参见内部异常。

In execution, getting error like "A call to SSPI failed, see inner exception".

我做的任何错误或遗漏在这里。任何证书需要安装在Windows?请帮我。

I'm doing any wrong or missed here. Any certificate installation is required in windows? Please help me.

推荐答案

我改变了我的code在PushNotification.class文件

I changed my code in PushNotification.class file

_certificate = string.IsNullOrEmpty(p12FilePassword)? new X509Certificate2(File.ReadAllBytes(p12File)): new X509Certificate2(File.ReadAllBytes(p12File), p12FilePassword);

_certificate = string.IsNullOrEmpty(p12FilePassword) ? new X509Certificate2(File.ReadAllBytes(p12File)) : new X509Certificate2(File.ReadAllBytes(p12File), p12FilePassword, X509KeyStorageFlags.MachineKeySet);

这篇关于在C#苹果推送通知服务端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆