Original: http://apns-c-sharp-net-vikram-jain.blogspot.com
=======================please, Install your certificate *.p12 on the PC, and take the firend name use this for refernce. Please, set configuration file: <appSettings> <add key= "Friendname" value= "Apple Production IOS Push services:c Om. Abc. XYZ "/> <add key=" productionkeyfriendname "value=" Production "/> </appsettings>==============send, pus H as per below in your class-to-call APNs Class:cls_apns _cls_apns = new Class_apns (); Here pass the custom fiels as per seprated by ";" Ann ASSSIGB The value by "Key=value;key=value" _cls_apns. Pushmessage ("Message", "Devicetoken", 0, "id=12;name=abc"); ==================please create a class as per below (Class_ Apns.cs).: Using system;using system.collections.generic;using system.linq;using system.web;using System.net.security;using system.net.sockets;using system.security.cryptography.x509certificates;using System.text;using system.configuration;using system.data;using system.security.authentication;using System.IO; Namespace Push.class{public class Class_apns {String certificatename = ""; String certificatepwd = ""; String friendname = "Apple development IOS Push services:com. Abc. XYZ "; String productionkeyfriendname = "Production"; SslStream SslStream; Public Cls_apns () {friendname = configurationmanager.appsettings["Friendname"]. ToString (); Productionkeyfriendname = configurationmanager.appsettings["Productionkeyfriendname"]. ToString (); } public bool Connecttoapns () {x509certificate2collection certs = new X509Certificate2Collection (); ADD the Apple cert to our collection certs. ADD (Getservercert ()); Apple Development Server address string apshost; if (Getservercert (). ToString (). Contains (productionkeyfriendname)) Apshost = "gateway.push.apple.com"; else Apshost = "Gateway. sandbox.push.apple.com "; Create a TCP socket connection to the Apple server on port 2195 TcpClient TcpClient = new TcpClient (Apshost, 2195); Create a new SSL stream over the connection SslStream = new SslStream (Tcpclient.getstream ()); Authenticate using the Apple cert sslstream.authenticateasclient (apshost, Certs, Sslprotocols.default, False ); Pushmessage (); return true; } private X509Certificate Getservercert () {x509certificate test = new X509Certificate (); Open the CERT store on local machine X509store store = new X509store (storename.my, storelocation . CurrentUser); if (store! = null) {//store exists, so-open it and search through the certs for the Apple Cert Store. Open (Openflags.openexistingonly | OPENFLAGS.READONLY); X509Certificate2Collection CERTs = store. certificates; if (certs. Count > 0) {int i; for (i = 0; i < certs. Count; i++) {X509Certificate2 cert = certs[i]; if (cert. Friendlyname.contains (Friendname)) {//cert found, so return it. return certs[i]; }}} return test; } return test; } private byte[] Hextodata (string hexstring) {if (hexstring = = NULL) return null ; if (hexstring.length% 2 = = 1) hexstring = ' 0 ' + hexstring; Up-to-whether-pad the first or last byte byte[] data = new BYTE[HEXSTRING.LENGTH/2]; for (int i = 0; i < data. Length; i++) Data[i] = Convert.tobyte (hexstring.substring (i * 2, 2), 16); return data; } public bool Pushmessage (string Mess, string devicetoken, int Badge, string custom_field) { Connecttoapns (); list<string> Key_value_custom_field = new list<string> (); String Ctoken = Devicetoken; String Calert = Mess; int iBadge = Badge; Ready to create the push notification byte[] buf = new byte[256]; MemoryStream ms = new MemoryStream (); BinaryWriter bw = new BinaryWriter (MS); Bw. Write (new byte[] {0, 0, 32}); byte[] Devicetoken = Hextodata (Ctoken); Bw. Write (Devicetoken); Bw. Write ((byte) 0); Create the APNS PAYLOAD-NEW.CAF is a audio file saved in the application bundle on the device string msg = ""; msg = "{\" aps\ ": {\" alert\ ": \" "+ Calert +" \ ", \" badge\ ": \" "+ ibadge.tostring () +" \ ", \" sound\ ": \" Noti.aiff\"}"; String payloadmess = ""; if (string. Isnullorwhitespace (Custom_field) = = False) {list<string> List_custom_field = Custom_field . Split (';'). ToList (); if (List_custom_field.count > 0) {for (int indx = 0; indx < List_custom_field.cou nt indx++) {Key_value_custom_field = List_custom_field[indx]. Split (' = '). ToList (); if (Key_value_custom_field.count > 1) {if (payloadmess! = "") Payloa Dmess + = ","; Payloadmess + = "\" + key_value_custom_field[0]. ToString () + "\": \ "" + key_value_custom_field[1]. ToString () + "\" "; }}}} if (payloadmess! = "") {msg + = "," + payloadmess; } msg += "}"; Write the data out to the stream bw. Write ((Byte) msg. Length); Bw. Write (Msg. ToCharArray ()); Bw. Flush (); if (sslStream! = null) {Sslstream.write (Ms. ToArray ()); return true; } return false; } }}
Send Push notification on Apple (APNS) on c#.net