我在一个连接中逐个向用户发送通知。但用户并没有获得推动。这是我的代码
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
string certificatePath = System.Web.HttpContext.Current.Server.MapPath("/Path/Cert.p12");
string certificatePassword = "password";
// Encode a test message into a byte array.
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
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.Default, true);
}
catch (AuthenticationException ex)
{
client.Close();
return;
}
MemoryStream memoryStream = new MemoryStream();
foreach (var item in db.Devices)
{
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0); //The command
writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
String deviceId = item.device1;
byte[] b0 = HexString2Bytes(deviceId);
writer.Write(b0);
String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
writer.Write((byte)payload.Length); //payload length (big-endian second byte)
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
}
// Close the client connection.
client.Close();
如果我创建连接以发送每次推送,则用户会收到推送。如何向多台设备发送通知?