6

모든 사용자가 PushSharp를 사용하여 COS에서 iOS 기기로 푸시 알림을 보내는 것처럼 보입니다. 그러나 라이브러리에는 알림을 직접 보내는 대신 대기열이 있습니다. 즉, Windows 서비스 나 그 자체의 설명서에 따라 적절하게 호스팅 할 항목이 필요하다는 것을 의미합니다. 이는 과도한 부담입니다. 내 ASP.NET 웹 서비스에 들어오는 웹 요청이 있고 그 일환으로 즉시 밀어 넣기 알림을 보내려고합니다. 그처럼 간단합니다.대기열없이 C#에서 APNS 푸시 알림 (iOS)을 보내는 방법

누구나 PushSharp를 사용하여 즉시 (대기열 메커니즘을 우회하는) 푸시 알림을 보내는 방법이나 푸시 알림을 제대로 보내는 방법을 알 수 있습니까? 이미 JSON 메시지를 공식화하는 코드가 있지만 요청에 .p12 파일을 적용하는 방법을 모르겠습니다. 어떻게해야하는지 Apple 설명서를 찾을 수 없습니다.

+0

이 작업을 수행하는 방법을 알고 싶습니다. – MichaelMcCabe

+0

Apple에서 하나의 메시지를 열지 않고 오랫동안 열어 두어야하는 TCP 이진 채널이라고 말한 Apple의 설명서를 찾았습니다. 그래서 큐잉이 중요하다고 생각합니다. 애플은 당신이 DoS 공격을하지 않는다면 당신을 차단하겠다고한다. –

답변

1

알림을 푸는 방법을 찾으려고 많은 시간을 보냈다가 나에게 도움이되는 코드 조각을 발견했습니다.

먼저 인증서를 올바르게 설치했는지 확인하십시오. 여기에 도움이되는 링크가 있습니다. 이 코드는 개발 인증서 "애플 개발 IOS 푸시 서비스"에 대한 것을

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; 
    } 

참고 : 여기에 https://arashnorouzi.wordpress.com/2011/04/13/sending-apple-push-notifications-in-asp-net-%E2%80%93-part-3-apns-certificates-registration-on-windows/

내가 푸시 알림을 위해 사용되는 코드입니다.