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.

using Project.MoonAPNS;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates; 
public Int64 PushNotificationToApple(Messages messageObject)
{
            
Int64 result = 0;
            
Messages message = null;
            
try
            {
                
string strDeviceToken ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                
string strPushMessage = "Good Morning!";
                
var payload1 = new NotificationPayload(strDeviceToken, strPushMessage, 1,"default");
                payload1.AddCustom(
"RegionID""IDQ10150"); 
                
var p = new List<NotificationPayload> { payload1 };
                
string certificatePath = HttpContext.Current.Server.MapPath("/debug/ck.pfx");
                
var push = new PushNotification(true, certificatePath, "XXXXXX");
                
string strfilename = push.P12File;
                
var message1 = push.SendToApple(p);
                
foreach (var item in message1)
                {
                    result = 1;
                }
            }
            
catch (Exception ex)
            {
            }
       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.

Up Next
    Ebook Download
    View all
    Learn
    View all