Sunday, 10 September 2017

IOS Push Notification in C# using PushSharp


Here we learn more about sending notification through PushSharp in IOS devices using C#.

PushSharp is a server-side library which can use for sending Push Notification to IOS, Android devices(using Google Cloud Messaging), Windows phones and Blackberry devices.

PushSharp is a library which you can download from below link or you can also use this through NuGet packages.

https://github.com/Redth/PushSharp

It's very easy to use and very easy to install for IOS, Android, Windows and Blackberry devices.

To use this library to send notification in IOS devices, first of all, you need to create .p12 certificate file which is very easy to create in Mac

Below are the steps for your reference.

  1. Login to your IOS development center by using this address: https://developer.apple.com/devcenter/ios then click on "Certificates, Identifiers & Profiles".
  2. When you click on it one IOS Apps will open, which contains four options like Certificates, Identifiers, Devices and Provisioning Profiles, So please click on Identifiers.
  3. When you click on Identifiers IOS App ID's wizard will open and select your Application ID which you previously created for your application. In that screen click on edit button to make changes.
  4. When you click new wizard will open and that wizard has Push Notifications check box, So click on it and it will display Development SSL Certificate & Production SSL Certificate options. After this click on create certificates under Production SSL Certificate and click continue.
  5. After this click on "Choose File" button and select CSR (Certificate Signing Request) file and click on generate.
  6. Click on "Download" button to download your Push Notification Certificate and then click on done.
To follow above process you will easily create Certificate file in Mac system.

How to convert Push Notification Certificate as a .p12 File

  1. Go to Keychain Access and select your certificate which you have installed in Keychain Access previously then right click on it and click on "Export".
  2. When you click on export wizard will open which contains four options Save As, Tags, Where and File Format, provide certificate name as per your requirement in Save As box, provide location where you want to save your file in Where box, make sure the File Format is set to "Personal Information Exchange" (.p12) click on save button to save it to your system.
  3. If it will ask for password leave it blank and click on Ok button.
  4. If it will ask for company password so enter it and click on Allow button.
  5. Then your .p12 file is ready and saved in the location which you specified.

If you follow above procedure correctly so your "Personal Information Exchange" (.p12) file is ready to use in our C# code.


use below code in C# to send Push Notification in IOS devices using PushSharp library.

#put your certificate file in configuration section so it will read and create a configuration#
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, Server.MapPath("AppCertificate/Certificate.p12"), "", true);

#Intialize ApnsServiceBroker to provide ApnsConfiguration#
var apnsBroker = new ApnsServiceBroker(config);

apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
{
     aggregateEx.Handle(ex =>
     {
  #See what kind of exception it was to further diagnose#
           if (ex is ApnsNotificationException)
           {
                var notificationException = (ApnsNotificationException)ex;                   
                if (ex.InnerException != null) {
                       Response.Write(ex.InnerException.ToString());
                 }

#Deal with the failed notification#
                var apnsNotification = notificationException.Notification;
                var statusCode = notificationException.ErrorStatusCode;
                Response.Write("Apple Notification Failed: ID= " + apnsNotification.Identifier + ",                             Code=" + statusCode);
             }
             else {
#Inner exception might hold more useful information like an ApnsConnectionException#
                     Response.Write("Notification Failed for some unknown reason : " + ex.InnerException);
              }
# Mark it as handled#
                    return true;
    });
};

apnsBroker.OnNotificationSucceeded += (notification) =>
{
     Response.Write("Apple Notification Sent!");
 };
 #Start the broker#
apnsBroker.Start();
#Queue a notification to send#
apnsBroker.QueueNotification(new ApnsNotification
{
    DeviceToken = #Registration_Id#,
    Payload = JObject.Parse("{\"aps\":{\"alert\":\"" + Server.HtmlEncode(description) + 
                                                        "\",\"badge\":\"" + 5 + "\",\"sound\":\"noti.aiff\"}}")
                                              });

#Stop the broker, wait for it to finish#
#This isn't done after every message, but after you're done with the broker
            apnsBroker.Stop();
 }

Here we finished it up with coding part if you follow each and every step correctly so you are able to send Push Notification in IOS devices using PushSharp.

Let us know if I miss anything above. Put your comments below related to any queries or any topic related to .Net, Android.

KEEP CALM & READ ON!!!