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));
}
}
}
-----------------------------------------------------------------

July 13, 2011

Passing Arrays Using ref and out

Like all out parameters, an out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee. For example:

public static void MyMethod(out int[] arr)
{
arr = new int[10]; // definite assignment of arr
}

Like all ref parameters, a ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee. A ref parameter of an array type may be altered as a result of the call. For example, the array can be assigned the null value or can be initialized to a different array. For example:

public static void MyMethod(ref int[] arr)
{
arr = new int[10]; // arr initialized to a different array
}

The following two examples demonstrate the difference between out and ref when used in passing arrays to methods.

Example 1

In this example, the array myArray is declared in the caller (the Main method), and initialized in the FillArray method. Then, the array elements are returned to the caller and displayed.

// cs_array_ref_and_out.cs
using System;
class TestOut
{
static public void FillArray(out int[] myArray)
{
// Initialize the array:
myArray = new int[5] {1, 2, 3, 4, 5};
}

static public void Main()
{
int[] myArray; // Initialization is not required

// Pass the array to the callee using out:
FillArray(out myArray);

// Display the array elements:
Console.WriteLine("Array elements are:");
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}

Output

Array elements are:
1
2
3
4
5

Example 2

In this example, the array myArray is initialized in the caller (the Main method), and passed to the FillArray method by using the ref parameter. Some of the array elements are updated in the FillArray method. Then, the array elements are returned to the caller and displayed.

// cs_array_ref_and_out2.cs
using System;
class TestRef
{
public static void FillArray(ref int[] arr)
{
// Create the array on demand:
if (arr == null)
arr = new int[10];
// Otherwise fill the array:
arr[0] = 123;
arr[4] = 1024;
}

static public void Main ()
{
// Initialize the array:
int[] myArray = {1,2,3,4,5};

// Pass the array using ref:
FillArray(ref myArray);

// Display the updated array:
Console.WriteLine("Array elements are:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}

Output

Array elements are:
123
2
3
4
1024

July 08, 2011

CAST() and CONVERT()

Both these functions are used to convert values from one datatype to another
But there are some differences between them

1 CAST is ANSI standard and CONVERT is specific to SQL Server
2 CAST can't be used for formating purposes.
But CONVERT can be used for formating purposes particularly on datetime and money datatype