notification developer clave autenticación apple apns c# ios push-notification apple-push-notifications pushsharp

c# - developer - xamarin ios push notifications azure



Cómo enviar una notificación push APNS(iOS) desde C#sin hacer cola (1)

Pasé muchas horas tratando de encontrar una forma de enviar notificaciones, luego encontré un código que lo hizo por mí.

En primer lugar, asegúrese de haber instalado los certificados correctamente, aquí hay un enlace que lo ayudará. https://arashnorouzi.wordpress.com/2011/04/13/sending-apple-push-notifications-in-asp-net-%E2%80%93-part-3-apns-certificates-registration-on-windows /

Aquí hay un código que utilicé para enviar notificaciones:

public static bool ConnectToAPNS(string deviceId, string message) { X509Certificate2Collection certs = new X509Certificate2Collection(); // Add the Apple cert to our collection certs.Add(getServerCert()); // Apple development server address string apsHost; /* if (getServerCert().ToString().Contains("Production")) 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 sslStream1 = new SslStream(tcpClient.GetStream()); // Authenticate using the Apple cert sslStream1.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false); PushMessage(deviceId, message, sslStream1); return true; } private static X509Certificate getServerCert() { X509Certificate test = new X509Certificate(); //Open the cert store on local machine X509Store store = new X509Store(StoreLocation.CurrentUser); if (store != null) { // store exists, so open it and search through the certs for the Apple Cert store.Open(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("Apple Development IOS Push Services")) { //Cert found, so return it. Console.WriteLine("Found It!"); return certs[i]; } } } return test; } return test; } private static byte[] HexToData(string hexString) { if (hexString == null) return null; if (hexString.Length % 2 == 1) hexString = ''0'' + hexString; // Up to you whether to 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; }

Tenga en cuenta que este código es para certificados de desarrollo "Apple Development IOS Push Services".

Parece que todos usan PushSharp para enviar notificaciones push a dispositivos iOS desde C #. Pero esa biblioteca tiene una cola que utiliza en lugar de enviar la notificación directamente, lo que significa que necesita un servicio de Windows o algo para alojarlo correctamente (según su propia documentación), que es excesivo para mí. Tengo una solicitud web entrante para mi servicio web ASP.NET y, como parte de su gestión, deseo enviar inmediatamente una notificación push. Simple como eso.

¿Puede alguien decirme cómo usar PushSharp para enviar uno inmediatamente (sin pasar por su mecanismo de cola) o cómo enviar correctamente la notificación de inserción? Ya tengo el código que formula el mensaje JSON, pero no sé cómo aplicar el archivo .p12 a la solicitud. No puedo encontrar ninguna documentación de Apple sobre cómo hacer eso.