Appleは、プロバイダがどのようにサービスに接続して通信しているかについての悪いドキュメントを実際に持っていました(2009年時点)。私はプロトコルについて混乱しています。そこにいる誰かが、これがどのように行われたかのC#サンプルを提供できれば、非常に感謝しています。誰かがApple Push Notification ProviderをC#で書く方法を知っていますか?
答えて
操作コード例:
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
//load certificate
string certificatePath = @"cert.p12";
string certificatePassword = "";
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.Tls, true);
}
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 deviceId = "DEVICEIDGOESHERE";
writer.Write(ToByteArray(deviceId.ToUpper()));
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();
私はこれが適切であることを望みますが、ちょうどJava用のものを作成しましたので、概念的にはC#と非常に似ています(おそらくSSLのものを除きますが、サンプルメッセージペイロードおよび暗号化設定:
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
char []passwKey = "<keystorePassword>".toCharArray();
KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("/path/to/apn_keystore/cert.p12"), passwKey);
KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts,passwKey);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(tmf.getKeyManagers(), null, null);
SSLSocketFactory factory =sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket(hostname,port); // Create the ServerSocket
String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
//start handshake
socket.startHandshake();
// Create streams to securely send and receive data to the server
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// Read from in and write to out...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(0); //The command
System.out.println("First byte Current size: " + baos.size());
baos.write(0); //The first byte of the deviceId length
baos.write(32); //The deviceId length
System.out.println("Second byte Current size: " + baos.size());
String deviceId = "<heaxdecimal representation of deviceId";
baos.write(hexStringToByteArray(deviceId.toUpperCase()));
System.out.println("Device ID: Current size: " + baos.size());
String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";
System.out.println("Sending payload: " + payload);
baos.write(0); //First byte of payload length;
baos.write(payload.length());
baos.write(payload.getBytes());
out.write(baos.toByteArray());
out.flush();
System.out.println("Closing socket..");
// Close the socket
in.close();
out.close();
}
再度、ないC位が、少なくともAppleが提供することが貧弱にObjCサンプルよりも近い
私はまだこれを動作させようとしています。私はあなたのコードをC#で複製しましたが、.NETはSSL "SSLStream"を介して接続するために別の種類のオブジェクトを使用しているので、 "ハンドシェイク"メソッドはありません。私は適切な握手を起こす方法を理解しているようには思えません。 – Phobis
ここをクリックしてください:http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx(ページの約4分の3、C#SSLクライアントハンドシェイクの例があり、SSLStream (コールバック) – Chaos
これは間違っていますそれはサーバーソケットを作成していませんクライアントソケットを作成していますtsとtmfは「信頼ストア"と"トラストマネージャファクトリ "ですが、ここではクライアントキーマテリアルを参照しています...本当に奇妙なものです – erickson
最高のAPNSSharpプロジェクトはGithubで利用できます。それはほんの数分で私のために絶対にうまくいった!
PushSharpライブラリはGitHubで使用できます。
私は私の意見では私のプロジェクトのすべての
public ActionResult ios()
{
string message = string.Empty;
var certi = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Certificates2.p12");
var appleCert = System.IO.File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Certificates2.p12"));
ApnsConfiguration apnsConfig = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, "Password");
var apnsBroker = new ApnsServiceBroker(apnsConfig);
apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
{
aggregateEx.Handle(ex =>
{
if (ex is ApnsNotificationException)
{
var notificationException = (ApnsNotificationException)ex;
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
var inner = notificationException.InnerException;
message = "IOS Push Notifications: Apple Notification Failed: ID=" + apnsNotification.Identifier + ", Code=" + statusCode + ", Inner Exception" + inner;
}
else
{
message = "IOS Push Notifications: Apple Notification Failed for some unknown reason : " + ex.InnerException;
}
return true;
});
};
apnsBroker.OnNotificationSucceeded += (notification) =>
{
message = "IOS Push Notifications: Apple Notification Sent!";
};
apnsBroker.Start();
try
{
string deviceToken = "33c2f3a13c90dc62660281913377c22066c1567e23c2ee2c728e0f936ff3ee9b";
apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = deviceToken,
Payload = JObject.Parse("{\"aps\":{\"alert\":\" Test Message\", \"badge\":1, \"sound\":\" default\",}}")
});
}
catch (Exception ex)
{
Console.Write(ex);
}
apnsBroker.Stop();
return View(message);
}
このコードは疑問に答えるかもしれませんが、他のものを導入せずにその問題をどのように解決するのか、なぜそれを使用するのか説明する方が良いでしょう。コードのみの回答は長期的には有用ではありません。 – Mateus
- 1. Apple Push Notification Bulk
- 2. Apple Push Notificationサービスアイドルタイムアウト
- 3. Apple Push Notificationサービス
- 4. Java APNS(Apple Push Notification Service)エラー
- 5. は、Apple Push Notificationサービスに
- 6. iPhoneアプリからApple Push Notificationをテストする方法は?
- 7. Apple push notification one app more subdomains
- 8. Apple Push Notificationサービス統計(apns)
- 9. Apple Push Notification Service with Python
- 10. 誰かがonActivityReenterを正しく使う方法を知っていますか?
- 11. Apple Push Notification sandboxを仮想デバイスとして使用する方法は?
- 12. 誰かが素早く汚いテキスト/文法パーサーを知っていますか?
- 13. JavaでApple Push Notificationサービスを実装するのに役立つ
- 14. サーバーからクライアントに通知を送信するApple Push Notification以外のオプションはありませんか?
- 15. 誰もBitmapFieldの表示方法を知っていますか?
- 16. 誰でもObjective-C Stylus Parserを知っていますか?
- 17. 誰かがソフトキーボードを作る方法を知っていますか?
- 18. 誰かがiphoneアプリでfbファンページをWebページから開く方法を知っていますか?
- 19. Apple Push Notification iPhone 4sの画面をロックする
- 20. 誰かがC#経由でProject Server 2003のプロジェクト/タスクを作成する方法を知っていますか?
- 21. Apple Push Notification - エンドユーザがCAとして行動しようとしました
- 22. JavaによるApple Push Notification ServerとのSSLハンドシェイク
- 23. 誰もC#BDD(バイナリデシジョンダイアグラム)パッケージを知っていますか?
- 24. 誰かが良いRuby ePubライブラリを知っていますか?
- 25. 誰かがタブバーの良いチュートリアルを知っていますか?
- 26. 誰かが良いT3(weblogic)ソースを知っていますか?
- 27. フレックス:誰かが良いHTMLエディタを知っていますか?
- 28. Push Notification in android c2dm
- 29. 誰かがSphinxを使ってC++プロジェクトを文書化していますか?
- 30. カスタムデータを送信するApple Push通知
でそれを使用しています、Appleのドキュメントはかなり明確である: http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/ RemoteNotificationsPG/CommunicatingWithAPS/CommunicatingWithAPS.html#// apple_ref/doc/uid/TP40008194-CH101-SW1 – Linulin
この回答は以前から選択されていました。また、shayからの答えを見てください - http://stackoverflow.com/a/36963561/19854このコードを書く手作業を探しているなら、もともと選んだ答えを見てください:http://stackoverflow.com/a/1077664/19854 – Phobis