加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中的Apple推送通知服务器端

发布时间:2020-12-15 07:50:50 所属栏目:百科 来源:网络整理
导读:我是.net的新手,在C#中做apn服务器端.我正在使用下面的代码将消息推送到苹果服务器. private void pushMessage() { int port = 2195; String deviceID = "4564c705 63b371aa 3811699e 1e4ac3d2 ba592b27 f2a5a613 d25cd035 xx213e54"; String hostname = "ga
我是.net的新手,在C#中做apn服务器端.我正在使用下面的代码将消息推送到苹果服务器.
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(),new RemoteCertificateValidationCallback(ValidateServerCertificate),null);

        SslStream sslStream = new SslStream(client.GetStream(),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();
    }

在执行过程中,收到错误,如“对SSPI的调用失败,请参阅内部异常”.

我做错了或错过了. Windows中是否需要安装任何证书?请帮我.

解决方法

我在PushNotification.class文件中更改了我的代码
_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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读