Ios Apple wallet(pkpass)的推送通知在.net服务器上不起作用
我必须在iOS设备上发送PKPass推送通知 使用SendEmptyPushNotification方法发送推送通知。我正在为deviceIdentifier发送PushTokenIos Apple wallet(pkpass)的推送通知在.net服务器上不起作用,ios,.net,apple-push-notifications,wallet,passkit,Ios,.net,Apple Push Notifications,Wallet,Passkit,我必须在iOS设备上发送PKPass推送通知 使用SendEmptyPushNotification方法发送推送通知。我正在为deviceIdentifier发送PushToken 但它不起作用。有时这段代码花费了太多的时间,有时根本不花时间。但在这两种情况下,我都无法在iOS设备上获得任何推送通知。我真的被困在这里了。有人能帮忙吗 public class IPhonePushNotificationService { public void SendEmptyPushNotific
但它不起作用。有时这段代码花费了太多的时间,有时根本不花时间。但在这两种情况下,我都无法在iOS设备上获得任何推送通知。我真的被困在这里了。有人能帮忙吗
public class IPhonePushNotificationService
{
public void SendEmptyPushNotification(string deviceIdentifier, string thumbprint)
{
string server = "gateway.push.apple.com";
using (TcpClient tcpClient = new TcpClient(server, 2195))
{
Trace.TraceInformation("Opening SSL Connection...");
using (SslStream sslStream = new SslStream(tcpClient.GetStream()))
{
try
{
X509Certificate2Collection certs = new X509Certificate2Collection();
Trace.TraceInformation("Adding certificate to connection...");
X509Certificate cert = GetAppleServerCert(thumbprint);
certs.Add(cert);
Trace.TraceInformation("Authenticating against the SSL stream...");
sslStream.AuthenticateAsClient(server, certs, SslProtocols.Default, false);
}
catch (AuthenticationException exp)
{
Trace.TraceError("Failed to authenticate to APNS - {0}", exp.Message);
return;
}
catch (IOException exp)
{
Trace.TraceError("Failed to connect to APNS - {0}", exp.Message);
return;
}
byte[] buf = new byte[256];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32 });
byte[] deviceToken = HexToData(deviceIdentifier);
bw.Write(deviceToken);
string msg = "{}";
bw.Write(new byte[] { 0, 2 });
bw.Write(msg.ToCharArray());
bw.Flush();
Trace.TraceInformation("Message sent. Closing stream...");
if (sslStream != null)
{
sslStream.Write(ms.ToArray());
}
byte[] response = new byte[6];
sslStream.Read(response, 0, 6);
sslStream.Flush();
}
}
}
private static X509Certificate GetAppleServerCert(string thumbprint)
{
X509Store store;
store = new X509Store(StoreLocation.CurrentUser);
if (store != null)
{
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates;
if (certs.Count > 0)
{
for (int i = 0; i < certs.Count; i++)
{
X509Certificate2 cert = certs[i];
if (cert.Thumbprint.Equals(thumbprint, StringComparison.InvariantCultureIgnoreCase))
{
return certs[i];
}
}
}
}
Trace.TraceError("Could not find the certification containing: {0} ", "R5QS56362W:R5QS56362W");
throw new InvalidDataException("Could not find the Apple Push Notification certificate");
}
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;
}
公共类IPhonePushNotificationService
{
公共无效SendEmptyPushNotification(字符串设备标识符、字符串指纹)
{
string server=“gateway.push.apple.com”;
使用(TcpClient TcpClient=新的TcpClient(服务器,2195))
{
Trace.TraceInformation(“打开SSL连接…”);
使用(SslStream SslStream=新的SslStream(tcpClient.GetStream())
{
尝试
{
X509Certificate2Collection certs=新的X509Certificate2Collection();
Trace.TraceInformation(“将证书添加到连接…”);
X509证书证书证书=GetAppleServerCert(指纹);
证书。添加(证书);
Trace.TraceInformation(“针对SSL流进行身份验证…”);
sslStream.authenticatesclient(服务器、证书、SslProtocols.Default、false);
}
捕获(AuthenticationException exp)
{
Trace.TraceError(“未能验证到APNS-{0}”,exp.Message);
返回;
}
捕获(IOEXP异常)
{
Trace.TraceError(“未能连接到APNS-{0}”,exp.Message);
返回;
}
字节[]buf=新字节[256];
MemoryStream ms=新的MemoryStream();
BinaryWriter bw=新的BinaryWriter(毫秒);
Write(新字节[]{1,0,0,0,0,0,0,0,0,0,0,32});
字节[]deviceToken=HexToData(deviceIdentifier);
写入(deviceToken);
字符串msg=“{}”;
Write(新字节[]{0,2});
Write(msg.ToCharArray());
bw.Flush();
Trace.TraceInformation(“消息已发送。正在关闭流…”);
如果(sslStream!=null)
{
sslStream.Write(ms.ToArray());
}
字节[]响应=新字节[6];
sslStream.Read(响应,0,6);
sslStream.Flush();
}
}
}
私有静态X509证书GetAppleServerCert(字符串指纹)
{
X509商店;
store=新的X509Store(StoreLocation.CurrentUser);
如果(存储!=null)
{
打开(OpenFlags.ReadOnly);
X509Certificate2Collection certs=存储证书;
如果(证书计数>0)
{
对于(int i=0;i
您使用的是什么证书?您需要使用PassTypeID证书连接到APNS。是的,我使用的是PassTypeID证书。在创建证书时,它仅用于生产,没有开发选项。我在服务器上使用http。这可能是一个问题吗?是的,除非您有问题,否则http将是一个问题在iPhone的开发者选项中为钱包启用了http。是的,在iPhone的开发者选项中启用了http。pkpass的其他方法(如注册和更新)工作正常,每次在iPhone中添加pass时,我都能获得推送令牌。这只是推送通知不起作用。我还注意到一件事,当我添加来自WWDR expiry 23的证书。发送通知代码崩溃。找不到“显示异常”证书。请确保指纹和证书位置值正确。