2017-08-01 64 views
3

我正在创建一个web api应用程序,并且我想向iOS设备发送通知。如何使用C#从APNS获得通知响应

我试过用pushsharp,但它工作正常,只有很少的设备,而发送到多个设备与过期/无效令牌它不发送通知到所有设备。

所以,我用下面的代码去:

public async Task pushMessage(string deviceToken, string message) 
{ 
    int port = 2195; 
    String hostname = "gateway.sandbox.push.apple.com"; 

    String certificatePath = "~/test.p12" // my certificate path 

    X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), CommonSource.GetAppleCertificatePassword(), X509KeyStorageFlags.MachineKeySet); 
    X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate); 

    TcpClient client = new TcpClient(hostname, port); 
    SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 
    try 
    { 
    sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false); 
    MemoryStream memoryStream = new MemoryStream(); 
    BinaryWriter writer = new BinaryWriter(memoryStream); 
    writer.Write((byte)0); 
    writer.Write((byte)0); 
    writer.Write((byte)32); 

    writer.Write(HexStringToByteArray(deviceToken.ToUpper()));  
    String payload = message.ToString(); 

    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(); 

    // Read message from the server. 
    //byte[] response = ReadMessage(sslStream); 

    client.Close(); 
    } 
    catch (System.Security.Authentication.AuthenticationException ex) 
    { 
    LogError(ex.Message); 
    client.Close(); 
    } 
    catch (Exception e) 
    { 
    LogError(e.Message); 
    client.Close(); 
    }    
} 

此代码的工作正是我想要的。
但现在我想检查来自APNS服务器的响应。所以我用byte[] response = ReadMessage(sslStream); 检查,ReadMessage方法是这样的:

private byte[] ReadMessage(SslStream sslStream) 
{ 
    MemoryStream ms = new MemoryStream(); 

    byte[] buffer = new byte[1024]; 

    int bytes = -1; 
    do 
    { 
     bytes = sslStream.Read(buffer, 0, buffer.Length); 

     ms.Write(buffer, 0, bytes); 

    } while (bytes != 0); 

    return ms.ToArray(); 
} 

但是当我运行它,它停留在bytes = sslStream.Read(buffer, 0, buffer.Length); 我不能找出问题。

谁能告诉我,我的代码有什么问题?

+0

将OnNotificationFailed事件处理程序附加到PushSharp对象。 如果通知失败并且您可以记录相关错误,它将被击中。 – Ajay

+0

@Ajay:正如我在问题中提到的那样,在这个问题中,我并没有使用PushSharp。我的问题是关于从苹果服务器获取响应。尽管我已经尝试过,它给了我一个无效令牌的错误,并且它已经停止队列。 –

+0

@HinaKhuman重新下载您的证书和您的证书是无效的。我想, –

回答

4

根据这篇文章

The problem with Apples Push Notification Service… Solutions and Workarounds…

  1. 苹果永远不会发送该邮件成功的响应。

  2. 在您有机会向流发送更多通知之前,Apple关闭连接之前,可能不会立即返回错误响应。这些可能仍然“通过”的通知仍然处于闲置状态。他们从不承认或交付,并且从不为他们返回错误响应。

所以在您的要求按预期工作的情况下,不会有任何响应。这意味着您尝试阅读回复将无法获得任何内容,这是您无法越过阅读线时所遇到的情况。它正在等待可能永远不会到来的回应。

+0

谢谢,但苹果公司的文档。声明了其他内容,请参阅来自APNs ** https://developer.apple的** HTTP/2响应中的内容。com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#// apple_ref/doc/uid/TP40008194-CH11-SW1 –

+0

有没有进一步的解释? –