How to send Push Notifications from C# Application to iOS Devices. (PushSharp API)

We can send push notifications to iOS Devices from our c# Application by making use of a simple & effective c# Library called PushSharp. This Library is open source & can be downloaded from Nuget Package Manager as well. This library can be used to send push notifications to not only iOS device, but also windows, android & blackberry devices.

For iOS this library makes use of Apple Push Notification Service (APNS), for android it uses Google cloud messaging (GCM) & for windows it uses Microsoft push notification services (MPNS).
Source code of the library can be downloaded from (https://github.com/Redth/PushSharp) or else Nuget can also be used to include reference of the library in C# applications.
Sending Push Notification to iOS Devices through PushSharp:
  1. How to Send:
    1. Create our push services broker: var push = new PushBroker();
    2. Wire up the events for all the services that the Push broker class in PushSharp library registers:
        push.OnNotificationSent += NotificationSent;
        push.OnChannelException += ChannelException;
        push.OnServiceException += ServiceException;
        push.OnNotificationFailed += NotificationFailed;
        push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
        push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
        push.OnChannelCreated += ChannelCreated;
        push.OnChannelDestroyed += ChannelDestroyed;
      Any action can be performed under these events or we can use them to   track whether notification sent or not or to log any errors.
  2. Things required to send notification:
    1. Apple Certificate file for the app(for which notification need to be sent). This certificate can be Sandbox/Development or Production Certificate. Extension of certificate should be .p12 . Password to open that file is also required. Make sure you use the right Push certificate. Apple allows you to generate one for connecting to Sandbox, and one for connecting to Production. You must use the right one, to match the   provisioning profile you build your app with.
    2. Device Token : Each of the apple device will have a unique Device Token associated to it.
  3. Read Certificate File: var appleCert = File.ReadAllBytes("Your Certificate File Path");
  4. Register Apple Service : push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "Certifcate File Password"));
  5. Construct Apple Notification Object: AppleNotification notification = new AppleNotification().ForDeviceToken("Device Token").WithAlert("Hello World!").WithBadge(7) .WithSound("sound.caf");
  6. Send Notification: push.QueueNotification(notification);
  7. Stop Services and wait for the queues to drains: push.StopAllServices();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s