t 已经创建了一个使用APNS-SHARP向 iPhone 发送推送通知的服务。在关闭连接时,我收到一条错误 消息“连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败因为连接的主机未能响应 17.149.34.140:2195。” 下面是我的代码
public void SendPushNotificationToiPhone()
{
try
{
int iPhoneCount = 1;
NotificationService pushNotificationService = null;
using (WhatsNewAtDbEntities entityObject = new WhatsNewAtDbEntities())
{
var activeiPhoneDevices = from items in entityObject.PushNotifications
where items.IsActive == 1 &&
items.Make == 2
select items;
int activeiPhoneDevicesCount = activeiPhoneDevices.Count();
foreach (var activeDevicesDetails in activeiPhoneDevices)
{
string deviceToken = activeDevicesDetails.NotificationUrl;
int counter = 0;
using (WhatsNewAtDbEntities objnewent = new WhatsNewAtDbEntities())
{
List<ContentCount_Result> count = objnewent.ContentCount(activeDevicesDetails.UserID, activeDevicesDetails.TenantID).ToList<ContentCount_Result>();
counter = Convert.ToInt32(count[0].Notificationcount);
}
if (iPhoneCount == 1)
{
//True if you are using sandbox certificate, or false if using production
bool sandbox = Convert.ToBoolean(AzureData.GetConfigurationSetting("sandBoxCert"));
string p12File = AzureData.GetConfigurationSetting("certName");
string p12FilePassword = AzureData.GetConfigurationSetting("certPassword");
string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);
pushNotificationService = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);
pushNotificationService.SendRetries = 5; //5 retries before generating notificationfailed event
pushNotificationService.ReconnectDelay = 5000; //5 seconds
}
Notification alertNotification = new Notification(deviceToken);
if (counter > 0)
{
alertNotification.Payload.Badge = counter;
}
else if (counter == 0)
{
//for resseting the badge value to 0(zero).
alertNotification.Payload.Badge.GetValueOrDefault(0);
}
//Queue the notification to be sent
pushNotificationService.QueueNotification(alertNotification);
if (iPhoneCount == activeiPhoneDevicesCount)
{
//First, close the service.
pushNotificationService.Close(); --**Getting error here**
////Clean up
pushNotificationService.Dispose();
}
iPhoneCount++;
}
}
}
catch (Exception)
{
}
}