使用C#的iPhone推送通知问题我无法向iphone发送通知。一切似乎都很好,因为服务运行良好,但请为iphone留言。这是代码:using(NetworkStreamnetworkStream=client.GetStream()){Console.WriteLine("Clientconnected.");//X509CertificateclientCertificate=newX509Certificate(@"C:UsersyagizozturkDocumentsVisualStudio2010ProjectsGarantiKampanyaGaranti.Web.Serviceapns-prod.pem","");X509CertificateclientCertificate=newX509Certificate(@"C:UsersyagizozturkDocumentsVisualStudio2010ProjectsGarantiKampanyaGaranti.Web.Serviceapns-prod-cert.p12","1234567");X509CertificateCollectionclientCertificateCollection=newX509CertificateCollection(newX509Certificate[1]{clientCertificate});//创建一个将关闭客户端流的SSL流。SslStreamsslStream=newSslStream(client.GetStream(),false,newRemoteCertificateValidationCallback(ValidateServerCertificate),null);尝试{sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com",clientCertificateCollection,SslProtocols.Default,false);}catch(AuthenticationExceptione){Console.WriteLine("异常:{0}",e.Message);if(e.InnerException!=null){Console.WriteLine("内部异常:{0}",e.InnerException.Message);}Console.WriteLine("认证失败-关闭连接。");客户端.Close();返回;}}}字符串标记="f5aaa8a729613480c79270085e881c435535dac4704dc00b082bfa8fc84aca10";//字符串令牌=“f5aaa8a729613480c79270085e881c435535dac4704dc00b082bfa8fc84aca90”;System.Text.ASCIIEncoding编码=newSystem.Text.ASCIIEncoding();byte[]myByteArray=newbyte[10000000];myByteArray=encoding.GetBytes(token);//inti=0;//foreach(charcintoken.ToCharArray())//{//myByteArray[i]=(byte)c;//我++;//}GeneratePayload(myByteArray,"test","default");}privatestaticbyte[]GeneratePayload(byte[]deviceToken,stringmessage,stringsound){MemoryStreammemoryStream=newMemoryStream();//命令memoryStream.WriteByte(0);byte[]tokenLength=BitConverter.GetBytes((Int16)32);大批。反向(令牌长度);//设备令牌长度memoryStream.Write(tokenLength,0,2);//令牌memoryStream.Write(deviceToken,0,32);//字符串长度stringapnMessage=string.Format("{{"aps":{{"alert":{{"body":"{0}","action-loc-key":null}},"sound":"{1}"}}}}",信息,声音);byte[]apnMessageLength=BitConverter.GetBytes((Int16)apnMessage.Length);Array.Reverse(apnMessageLength);//消息长度memoryStream.Write(apnMessageLength,0,2);//写入消息memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage),0,apnMessage.Length);返回memoryStream.ToArray();}//以下方法由RemoteCertificateValidationDelegate调用。publicstaticboolValidateServerCertificate(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorssslPolicyErrors){if(sslPolicyErrors==SslPolicyErrors.None)返回真;Console.WriteLine("证书错误:{0}",sslPolicyErrors);//不允许这样客户端与未经身份验证的服务器进行通信。返回假;我在这里得到的答案是从c#发送推送通知的完整代码。以上就是C#学习教程:iPhone推送通知使用c#的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——publicboolConnectToAPNS(){X509Certificate2Collectioncerts=newX509Certificate2Collection();//将Apple证书添加到我们的集合中certs.Add(getServerCert());//苹果开发服务器地址stringapsHost;如果(getServerCert().ToString().Contains("Production"))apsHost="gateway.push.apple.com";elseapsHost="gateway.sandbox.push.apple.com";//在端口2195上创建到Apple服务器的TCP套接字连接TcpClienttcpClient=newTcpClient(apsHost,2195);//通过连接创建一个新的SSL流sslStream=newSslStream(tcpClient.GetStream());//使用Apple证书进行身份验证sslStream.AuthenticateAsClient(apsHost,certs,SslProtocols.Default,false);//推送消息();返回真;}privateX509CertificategetServerCert(){X509Certificatetest=newX509Certificate();//在本地机器上打开证书存储X509Storestore=newX509Store(StoreLocation.CurrentUser);if(store!=null){//store存在,所以打开它并在证书中搜索AppleCertstore.Open(OpenFlags.ReadOnly);X509Certificate2Collectioncerts=store.Certificates;if(certs.Count>0){inti;for(i=0;i本文采集自网络,不代表立场,如涉及侵权,请点右联系管理员删除,如需转载请注明出处:
