And*_*ott 6 c# push-notification apple-push-notifications ios pushsharp
似乎每个人都使用PushSharp从C#向iOS设备发送推送通知.但是该库有一个它使用的队列,而不是直接发送通知,这意味着您需要一个Windows服务或其他东西来正确托管它(根据自己的文档)这对我来说是过度的.我有一个传入Web请求到我的ASP.NET Web服务,作为处理它的一部分,我想立即发送推送通知.就那么简单.
任何人都可以告诉我如何使用PushSharp立即发送一个(绕过其队列机制)或如何正确发送推送通知自己?我已经有了制定JSON消息的代码,但我不知道如何将.p12文件应用于请求.我找不到任何Apple文档来说明如何做到这一点.
小智 -2
我花了很多时间试图找到一种推送通知的方法,然后我找到了一段可以帮我做到这一点的代码。
首先确保您正确安装了证书,这里有一个可以帮助您的链接。 https://arashnorouzi.wordpress.com/2011/04/13/sending-apple-push-notifications-in-asp-net-%E2%80%93-part-3-apns-certificates-registration-on-windows /
这是我用来推送通知的代码:
public static bool ConnectToAPNS(string deviceId, string message)
{
X509Certificate2Collection certs = new X509Certificate2Collection();
// Add the Apple cert to our collection
certs.Add(getServerCert());
// Apple development server address
string apsHost;
/*
if (getServerCert().ToString().Contains("Production"))
apsHost = "gateway.push.apple.com";
else*/
apsHost = "gateway.sandbox.push.apple.com";
// Create a TCP socket connection to the Apple server on port 2195
TcpClient tcpClient = new TcpClient(apsHost, 2195);
// Create a new SSL stream over the connection
SslStream sslStream1 = new SslStream(tcpClient.GetStream());
// Authenticate using the Apple cert
sslStream1.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false);
PushMessage(deviceId, message, sslStream1);
return true;
}
private static X509Certificate getServerCert()
{
X509Certificate test = new X509Certificate();
//Open the cert store on local machine
X509Store store = new X509Store(StoreLocation.CurrentUser);
if (store != null)
{
// store exists, so open it and search through the certs for the Apple Cert
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates;
if (certs.Count > 0)
{
int i;
for (i = 0; i < certs.Count; i++)
{
X509Certificate2 cert = certs[i];
if (cert.FriendlyName.Contains("Apple Development IOS Push Services"))
{
//Cert found, so return it.
Console.WriteLine("Found It!");
return certs[i];
}
}
}
return test;
}
return test;
}
private static byte[] HexToData(string hexString)
{
if (hexString == null)
return null;
if (hexString.Length % 2 == 1)
hexString = '0' + hexString; // Up to you whether to pad the first or last byte
byte[] data = new byte[hexString.Length / 2];
for (int i = 0; i < data.Length; i++)
data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return data;
}
Run Code Online (Sandbox Code Playgroud)
请注意,此代码用于开发证书“Apple Development IOS Push Services”。
| 归档时间: |
|
| 查看次数: |
6404 次 |
| 最近记录: |