gpt4 book ai didi

c# - 如何使用 C# 从 APNS 获取通知响应

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:15:52 26 4
gpt4 key购买 nike

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

我尝试过使用 pushsharp,但它只适用于少数设备,而将它发送到具有过期/无效 token 的多个设备时,它不会向所有设备发送通知。

所以,我将使用以下代码:

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);
我无法弄清楚这个问题。

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

最佳答案

根据这篇文章

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

  1. Apple never sends a response for messages that succeeded.

  2. An error response may not immediately be returned before you have a chance to send more notifications to the stream, but before Apple closes the connection. These notifications that might still ‘make it through’ are left in limbo. They are never acknowledged or delivered, and never have error responses returned for them.

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

关于c# - 如何使用 C# 从 APNS 获取通知响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com