July 19, 2011

iPhone push notification using .NET,C#,ASP.NET

iPhone push notification
Step1: Download the project "Apns-Sharp-1.0.3.0-Source" from this link https://github.com/Redth/APNS-Sharp/downloads
Step2: Add "JdSoft.Apple.Apns.Notifications.dll" file reference to your project and follow the bellow code.
-----------------------------------------------------------------------------------

using System;
using JdSoft.Apple.Apns.Notifications;

namespace JdSoft.Apple.Apns.Test
{
class Program
{


STAThread]
static void Main(string[] args)
{
//Variables you may need to edit:
//---------------------------------

//True if you are using sandbox certificate, or false if using production
bool sandbox = false;

//Put your device token in here
string testDeviceToken = "f5e84401 5e1a2aac 2459d5c3 5c4941a4 e0384cfb 3a6ac3d2 68ab4643 e016c5dc";
//Put your PKCS12 .p12 or .pfx filename here.
// Assumes it is in the same directory as your app
string p12File = "apn_identity.p12";

//This is the password that you protected your p12File
// If you did not use a password, set it as null or an empty string
string p12FilePassword = "123sdf";

//Number of notifications to send
int count = 3;

//Number of milliseconds to wait in between sending notifications in the loop
// This is just to demonstrate that the APNS connection stays alive between messages
int sleepBetweenNotifications = 15000;


//Actual Code starts below:
//--------------------------------

string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);

NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);

service.SendRetries = 5; //5 retries before generating notificationfailed event
service.ReconnectDelay = 5000; //5 seconds

service.Error += new NotificationService.OnError(service_Error);
service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);

service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
service.Connecting += new NotificationService.OnConnecting(service_Connecting);
service.Connected += new NotificationService.OnConnected(service_Connected);
service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);

//The notifications will be sent like this:
// Testing: 1...
// Testing: 2...
// Testing: 3...
// etc...
for (int i = 1; i <= count; i++)
{
//Create a new notification to send
Notification alertNotification = new Notification(testDeviceToken);

alertNotification.Payload.Alert.Body = string.Format("Testing {0}...", i);
alertNotification.Payload.Sound = "default";
alertNotification.Payload.Badge = i;



//Queue the notification to be sent
if (service.QueueNotification(alertNotification))
Console.WriteLine("Notification Queued!");
else
Console.WriteLine("Notification Failed to be Queued!");

//Sleep in between each message
if (i < count)
{
Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification...");
System.Threading.Thread.Sleep(sleepBetweenNotifications);
}
}

Console.WriteLine("Cleaning Up...");

//First, close the service.
//This ensures any queued notifications get sent befor the connections are closed
service.Close();

//Clean up
service.Dispose();

Console.WriteLine("Done!");
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}

static void service_BadDeviceToken(object sender, BadDeviceTokenException ex)
{
Console.WriteLine("Bad Device Token: {0}", ex.Message);
}

static void service_Disconnected(object sender)
{
Console.WriteLine("Disconnected...");
}

static void service_Connected(object sender)
{
Console.WriteLine("Connected...");
}

static void service_Connecting(object sender)
{
Console.WriteLine("Connecting...");
}

static void service_NotificationTooLong(object sender, NotificationLengthException ex)
{
Console.WriteLine(string.Format("Notification Too Long: {0}", ex.Notification.ToString()));
}

static void service_NotificationSuccess(object sender, Notification notification)
{
Console.WriteLine(string.Format("Notification Success: {0}", notification.ToString()));
}

static void service_NotificationFailed(object sender, Notification notification)
{
Console.WriteLine(string.Format("Notification Failed: {0}", notification.ToString()));
}

static void service_Error(object sender, Exception ex)
{
Console.WriteLine(string.Format("Error: {0}", ex.Message));
}
}
}
-----------------------------------------------------------------

21 comments:

Anonymous said...

HI, I have implemented code as you mentioned but as I test this code on server, notification get queued but not received at iPhone device.
However from local machine everything working fine and notification is getting on device.
What could be the problem with server?
Any help would be highly appreciated

sivaramakrishnan said...

shiva Here,

I am also facing the same issue, I would like to know if you had resolved this issues

kindly share with me(frankshiva@gmail.com)

Anonymous said...

Hello, I have a problem with push notification, I can't regist .p12 file (error message "file is empty") But when i use it in my program and read it in byte array, i see that it is not empty. and after it method new X509Certificate2(byteArray, p12FilePassword) get an exception (wrong parameter).
Thanks

Anonymous said...

i also face the problem that notification is queued but not recieved

Unknown said...

I am not getting any error when running the code but I am getting a message like messages got queued and even the message is not getting delivered to iphone device .So what might be the problem.How to fix this issue.

Thanks in advance
Sushanth.

Anonymous said...

I have implemented code but I am getting following exception.
"A call to SSPI failed, see inner exception."

Anonymous said...

It is working perfect for me

Unknown said...

I have geeting the same error.
"A call to SSPI failed, see inner exception."

Plz help.

Safdar Ali said...

I want to disable some unwanted notifications from Yahoo and Facebook. How to disable them? notification software

Anonymous said...

I used the same code for my production mode keeping sandbox condition false but i am not getting any notification , but in development it is fine . let me know is there any thing to change to switch to production

Unknown said...

I would like to use this code in my application desktop alerting software

Rajendra Dhanora said...

ok Steve ..please go ahead and use this code in your application !!

Unknown said...

how can i send a push notification to several devices using .net?

Unknown said...

Hii, i have also problem for sending push notification on android device,
it says "notification get queued "

,Still not receiving on my apple Device, Can anyone give me solution.
Please give quick response.

Rohin Sharma said...

I used the same code for my production mode keeping sandbox condition false but i am not getting any notification , but in local server it is fine . please let me know i need its urgent to be in working mode.
Please contact me at rohin2sharma@gmail.com

Anonymous said...

I am getting error after using this code. Can you help me please.The error is party is not connected or canceled the connection.

Nidhi said...

I am also facing notification not show on mobile problem bt it is working on local...please give any solution for this problem on mishra.nidhi1991@gmail.com

Unknown said...

I am not getting any error when running the code but I am getting a message like messages got queued and even the message is not getting delivered to iphone device .So what might be the problem.How to fix this issue.

er.bhanu madan said...

I am getting the error message
"A call to SSPI failed, See inner exception"
can you please help?

nilanjan said...

I use the same code in production but after october 2014 this code is not working so could you please help me in providing some updated on the same.

Thanks
Nilanjan

Unknown said...

Authentication failed because remote party has closed the transport stream ??