私はユーザーに1つずつ通知を送信しますが、しばらく時間がかかります。私は個別に各デバイスに送信する必要があり、複数のデバイスに通知を送信するために この私のコード1回のリクエストで複数のデバイス(iOS)にプッシュ通知を送信するにはどうすればよいですか?
var deviceios = db.Devices;
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
string certificatePath = System.Web.HttpContext.Current.Server.MapPath("mypath/cert.p12);
string certificatePassword = "password";
foreach (var item in deviceios)
{
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, 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, System.Security.Authentication.SslProtocols.Default, false);
}
catch (AuthenticationException ex)
{
client.Close();
return;
}
//// Encode a test message into a byte array.
MemoryStream memoryStream = new MemoryStream();
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 devicetocken = item.device1;// iphone device token
byte[] b0 = HexString2Bytes(devicetocken);
WriteMultiLineByteArray(b0);
writer.Write(b0);
String payload;
string strmsgbody = "";
int totunreadmsg = 1;
strmsgbody = message;
payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"badge\":" + totunreadmsg.ToString() + ",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}";
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write((byte)b1.Length); //payload length (big-endian second byte)
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
try
{
sslStream.Write(array);
sslStream.Flush();
}
catch
{
}
client.Close();
。デバイスのリストに通知を送信できますか?
は、独自のサーバを使用してみてください。次回の更新では、Webサーバーをリッスンする関数を配置し、イベントが発生するとプッシュイベントを通知します。 – devRicher
万が一ASP.Net SignalRの使用を検討しましたか?それはその目的のためのものです。 –
自分のサーバーを使用しても、APNの動作は変わりません。また、SignalRを使用することはプッシュ通知を行うこととはまったく異なります。質問については、通知ごとに接続を開いたり閉じたりしていませんか?それは長い時間がかかります。通知は1つずつ送信する必要がありますが、1つの接続内で送信することができます。エラーメッセージを処理するだけです。 –