【问题标题】:How to get notification response from the APNS using C#如何使用 C# 从 APNS 获取通知响应
【发布时间】:2018-01-08 00:47:43
【问题描述】:

我正在创建一个 web api 应用程序,我想向 iOS 设备发送通知。

我已尝试使用 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); 我无法找出问题所在。

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

【问题讨论】:

  • 将 OnNotificationFailed 事件处理程序附加到 PushSharp 对象。如果通知失败,它将被击中,您可以记录相关错误。
  • @Ajay:正如我在问题中提到的,在这个问题中我并没有使用 PushSharp。我的问题是关于从苹果服务器获得响应。虽然我已经尝试过,但它给了我一个无效令牌的错误并且它已经停止了队列。
  • @HinaKhuman 重新下载您的证书,您的证书无效。我认为,
  • @JagadeeshGovindaraj:是的,我也试过了,但我收到了同样的错误。
  • @HinaKhuman.my 简单的答案是您的令牌从移动客户端无效。使用 FCM 对于 pushnotification.google 有不错的控制台向设备发送推送通知。

标签: c# ios push-notification apple-push-notifications


【解决方案1】:

根据这篇文章

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

  1. Apple 从不为成功的消息发送响应。

  2. 在您有机会向流发送更多通知之前,但在 Apple 关闭连接之前,可能不会立即返回错误响应。这些可能仍然“通过”的通知被搁置了。它们永远不会被确认或传递,也永远不会为它们返回错误响应。

因此,如果您的请求按预期工作,则不会有任何响应。这意味着您尝试读取响应将不会得到任何东西,这就是您无法通过读取线时所遇到的情况。它正在等待可能永远不会出现的响应。

【讨论】:

  • 谢谢,但苹果办公室文档。陈述其他内容,请参阅 HTTP/2 来自 APNs 的响应 developer.apple.com/library/content/documentation/…
  • 还有什么解释吗?
  • 在您的代码中,您使用的是旧版 Binary Provider API,该链接用于使用新的 HTTP/2 方法调用 APN,因此文档与您的代码无关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2014-04-19
  • 2012-09-04
相关资源
最近更新 更多