我尝试使用 apns-csharp 库从 .NET 发送推送通知,我在 Apple Provision Portal 上创建了证书,下载它并转换为 p12 格式,当我尝试使用代码加载它时:
private ActionResult SendAlertPushNotification(string appId, string notificationContent, bool useSandBox)
{
NotificationService notificationService = new NotificationService(useSandBox,ApplicationsRepository.GetAPNSCertificateForApplication(appId,useSandBox),"123",1);
notificationService.ReconnectDelay = 2000;
notificationService.Error += new NotificationService.OnError(service_Error);
notificationService.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);
notificationService.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
notificationService.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
notificationService.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
notificationService.Connecting += new NotificationService.OnConnecting(service_Connecting);
notificationService.Connected += new NotificationService.OnConnected(service_Connected);
notificationService.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
var devices = ApplicationsRepository.GetPushClientDevicesID(appId);
foreach (var token in devices)
{
var notification = new Notification(token);
notification.Payload.Alert.Body = notificationContent;
notification.Payload.Sound = "default";
notification.Payload.Badge = 1;
//Queue the notification to be sent
if (notificationService.QueueNotification(notification))
Debug.WriteLine("Notification Queued!");
else
Debug.WriteLine("Notification Failed to be Queued!");
}
notificationService.Close();
ViewData["app"] = ApplicationsRepository.GetApplicationByAppId(appId);
ViewData["count"] = devices.Count;
return View("SendSuccess");
}
我在尝试加载证书时收到内部错误。如果我使用 .cer 格式的原始证书,我不会收到任何异常,但实际上没有任何内容发送到 APNS 服务器。有人遇到过这个问题吗?
最佳答案
确保您导出了正确的证书。最近遇到了类似的问题,才发现我导出错了。
OSX Keychain After you've created the appropriate Push Notification Certificate in iPhone Developer Program Portal you should have downloaded a file named something like apn_developer_identity.cer. If you have not done so already, you should open/import this file into Keychain, into your login section.
Finally, if you filter Keychain to show your login container's Certificates, you should see your Certificate listed. Expand the certificate, and there should be a Key underneath/attached to it.
Right Click or Ctrl+Click on the appropriate Certificate and choose Export. Keychain will ask you to choose a password to export to. Pick one and remember it. You should end up with a .p12 file. You will need this file and the password you picked to use the Notification and Feedback Libraries here.
我确信现在您已经以某种方式解决了这个问题,但是我自己刚刚经历过这个问题,我认为放下我的经验可能会很好。
关于asp.net - apns-csharp证书问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955752/