Apple Push Notification Using ASP.Net

In this article, I would like to explain Apple Push Notification using ASP.Net. Push Notification is widely used in the iPhone development.
  1. Get or generate the certificate file (.cer) and Personal Information Exchange (.pem/.p12) file from the Apple Mac OS.
  2. Convert the .pem to a .pfx file because the ASP.Net Web API works with the .pfx file using SSLConverter.
  3. To convert the file go to https://ssl4less.eu/ssl-tools/convert-certificate.html#certificateConverter

    SSL Convert
     
  4. Now you have 2 files, in other words a certificate file (.cer) and a .pfx file. Install both files using Microsoft Management Console (MMC).
  5. Type mmc after pressing <Windows> + R.
     
    run MMC
     
  6. Press "OK" and a new screen will open.
     
    add snap-in in MMC
     
  7. Install the certificate (aps_development.cer ) and Personal Information Exchange files (.pfx, .p12) into the Personal-Certificates folder.
     
    Install certificate in MMC
     
    import certificate in MMC=
     
  8. After selecting "Import", it will ask where you need to install the certificate. Select the "Computer" account.
     
    location for MMC certificate
     
  9. Select "Local computer" -> "Finish-Certificates". Click on "Add" and then "Ok".
     
    give location for MMC certificate
     
    choose snap-ins for MMC
     
  10. Click "Browse" -> "Next" -> "Next" -> "By default selected". Place all certificates in the following store:
     
    choose certificate to import in MMC
     
    choose certificate to import in MMC
     
    choose certificate to import in MMC
     
    certificate import wizard
     
    choose certificate import store in MMC
     
  11. Click on "Finish" and the certificate file is successfully imported into the Personal-Certificates folder.
     
    choose certificate import store in MMC
     
    certificate import in MMC
     
    Repeat the same process to install the .pfx file into the Personal-Certificate folder.
     
    Also install both the files (.cer and .pfx) into the "Trusted Root Certification Authorities -Certificate" folder.
     
    certificate import to Trusted Root Certification Authorities
     
    This environment is ready to send Push Notification.
 
The following is the ASP.Net method to send Push Notification using ASP.Net via Apple.
 
Download the file Project.zip. Extract and add it your project. Build Project.MoonAPNS. Add a reference in your web application/web API. A simple class file for PushNotificationToApple is as follows.
  1. using Project.MoonAPNS;  
  2. using System.Net.Security;  
  3. using System.Net.Sockets;  
  4. using System.Security.Cryptography.X509Certificates;   
  5. public Int64 PushNotificationToApple(Messages messageObject)  
  6. {  
  7.             Int64 result = 0;  
  8.             Messages message = null;  
  9.             try  
  10.             {  
  11.                 string strDeviceToken ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  
  12.                 string strPushMessage = "Good Morning!";  
  13.                 var payload1 = new NotificationPayload(strDeviceToken, strPushMessage, 1,"default");  
  14.                 payload1.AddCustom("RegionID""IDQ10150");   
  15.                 var p = new List<NotificationPayload> { payload1 };  
  16.                 string certificatePath = HttpContext.Current.Server.MapPath("/debug/ck.pfx");  
  17.                 var push = new PushNotification(true, certificatePath, "XXXXXX");  
  18.                 string strfilename = push.P12File;  
  19.                 var message1 = push.SendToApple(p);  
  20.                 foreach (var item in message1)  
  21.                 {  
  22.                     result = 1;  
  23.                 }  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.             }  
  28.        return result;  
In the preceding class, strDeviceToken is a DeviceToken of length 64. Next XXXXXX is the password that was created at the time the certificate was generated using the Apple MAC OS.