I have been struggling to get push notifications to send from a C# console application. I have a working p12 certificate from apple's development website and i also have the device token for the device i want to send the notification to. Below is the code i am using to try and send the notification. I am using an older version of PushSharp (2.2.1.0) as I am using the same console application to send android push notifications which works 100%.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PushSharp;
using PushSharp.Android;
using PushSharp.Apple;
using PushSharp.Core;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net.Sockets;
namespace PushNotificationServiceTradeway
{
class Program
{
static void Main(string[] args)
{
var 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;
//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes("Certificate");
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, ""));
push.QueueNotification(new AppleNotification()
.ForDeviceToken("Phone_Token")
.WithAlert("Hello World!")
.WithBadge(7));
push.StopAllServices();
}
static void DeviceSubscriptionChanged(object sender,
string oldSubscriptionId, string newSubscriptionId, INotification notification)
{
Console.WriteLine(notification);
}
//this even raised when a notification is successfully sent
static void NotificationSent(object sender, INotification notification)
{
Console.WriteLine(notification);
}
//this is raised when a notification is failed due to some reason
static void NotificationFailed(object sender,
INotification notification, Exception notificationFailureException)
{
Console.WriteLine(notificationFailureException);
}
//this is fired when there is exception is raised by the channel
static void ChannelException
(object sender, IPushChannel channel, Exception exception)
{
Console.WriteLine(exception);
}
//this is fired when there is exception is raised by the service
static void ServiceException(object sender, Exception exception)
{
Console.WriteLine(exception);
}
//this is raised when the particular device subscription is expired
static void DeviceSubscriptionExpired(object sender,
string expiredDeviceSubscriptionId,
DateTime timestamp, INotification notification)
{
Console.WriteLine(notification);
}
//this is raised when the channel is destroyed
static void ChannelDestroyed(object sender)
{
Console.WriteLine(sender);
}
//this is raised when the channel is created
static void ChannelCreated(object sender, IPushChannel pushChannel)
{
Console.WriteLine(pushChannel);
}
}
}
But when i run the application to send the notification it hangs and times out after a minute with the following error in the console
System.Net.Sockets.Socketexception (0x80004005) a connection attempt failed bacause the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 17.172.232.45:2196 at System.Net.Sockets.TcpClient..ctor(String hostname, int32 port) at Pushsharp.Apple.FeedbackService.Run(ApplePushChannelSetings, CancellationToken cancelToken) at PushSharp.Apple.ApplePushService.(ApplePushService)c_AnonStorey0.()m_1(object state)
Any help or advice on how to fix/solve this would be appreciated.
Ok found the fix, it was the firewall settings at my company blocking access to Apple's push notification servers.