我正在使用 APNS-SHARP 推送通知。我正在使用带有生产令牌的分发环境和生产证书。以下是我的代码的一部分。
try
{
Response.Write(notifications.Count.ToString());
bool useSandbox = false;
string p12FilePath = Server.MapPath("/Notification/PushNotification.p12");
string p12FilePassword = "password";
using (NotificationChannel channel = new NotificationChannel(useSandbox, p12FilePath, p12FilePassword))
{
channel.SendNotifications(notifications.ToArray());
}
log.Info("Send Successful: " + recipientList.Count + " Record Sent.");
}
catch (Exception ex)
{
log.Info("Send Error: " + ex.ToString());
}
有一件奇怪的事情是布尔沙箱是真还是假。我总是在日志文件中收到“发送成功”消息。但是,设备没有收到任何通知。
我还尝试使用 PushSharp 发送具有相同证书的通知。下面是我的代码:
try
{
if (notifications.Count > 0)
{
bool useProduction = true;
string p12FilePath = Server.MapPath("/Notification/ESchoolProPushNotification.p12");
string p12FilePassword = "password1";
PushBroker push = new PushBroker();
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;
push.RegisterAppleService(new ApplePushChannelSettings(useProduction, p12FilePath, p12FilePassword));
foreach(AppleNotification an in notifications)
{
push.QueueNotification(new AppleNotification()
.ForDeviceToken("cedb76c24773c0f1f7688c3175a96959e5b434475501c09c4e17a2ff32706826")
.WithAlert("Hello World!")
.WithBadge(1)
.WithSound("default"));
}
push.StopAllServices();
log.Info("Send Successful: " + recipientList.Count + " IOS Record Sent.");
}
else
{
log.Info("No IOS Record Sent.");
}
代码将在 QueueNotification 函数中停留很长时间,无法进一步处理,但不会返回任何错误。我必须手动停止调试才能结束。
顺便说一句,我按照这些步骤创建 p12 证书。
- 将 apn_developer_identity.cer(der 格式)转换为 pem:openssl x509 -in apn_developer_identity.cer -inform DER -out apn_developer_identity.pem -outform PEM}
- 接下来,将 p12 私钥转换为 pem(需要输入至少 4 个字符的密码): openssl pkcs12 -nocerts -out private_dev_key.pem -in private_dev_key.p12
- (可选):如果要从私钥中删除密码: openssl rsa -out private_key_noenc.pem -in private_key.pem
- 获取证书和密钥(带或不带密码)并创建 PKCS#12 格式文件: openssl pkcs12 -export -in apn_developer_identity.pem -inkey private_key_noenc.pem -certfile CertificateSigningRequest??.certSigningRequest -name "apn_developer_identity" -out apn_developer_identity .p12
是p12证书问题吗?