Working With Push Notifications in iOS

Dheeraj Kumar Gunti

Reading Time : ( words)


Hi All, Welcome Back. Today we will see about Push Notifications in iOS Application.

In general scenario notifying updates to the millions of users is very tough. For notifying latest updates to the user Apple, Google and Microsoft came up with a concept of pushing notifications.

For Apple its APNS (Apple Push Notification Service), for Google its GCM (Google Cloud Messaging) and for Windows its WNS (Windows Push Notification Services).

Before starting to work on Push notifications we need to see about creating a certificate and provisioning profile for our application.

Login in to developer.apple.com

Click on Member Center on the menu.




Login with your apple developer account.




Click on Certificates Section once you are successfully logged in.



Click on Certificates on the left hand side menu. And Click on Add Button to create a new certificate.



Click on iOS Development and Click on Continue. You Will see Certificate Signing request screen as below.



Click on Continue and you see Upload CSR screen. Upload the signing request file which was created from your development Mac machine.



Once the upload is successful you will see a certificate created.



Click on the App Ids section on the left and choose the app id you have created. Push Notification Option should be enabled for working with push notifications.



You can create and download Apple Push Notification SSL from here.



Once the certificate is created, create provisioning profile using the same certificate and download them to your development machine. Export the certificate to generate a .p12 file which will be used in triggering notifications.

Note: For development you need to create one push notification certificate and for production you need to create another push notification certificate.

Registering device for Push Notification and getting device id:

In Appdelegate.cs, In FinishedLaunching method you need to register for push notifications.

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
var settings = UIUserNotificationSettings.GetSettingsForTypes (UIUserNotificationType.Sound |
              UIUserNotificationType.Alert, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
return true;
}

For getting the device id we need to override RegisterdForRemoteNotifications method and we need retrieve the device id.

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
var regId = deviceToken.DebugDescription.Replace ("<", "").Replace (">", "").Replace (" ", "");
Console.WriteLine ("Device Token Reg Id : " + regId);
}

Web Application:

This application is used to post messages to the devices registered.

//Create our push services broker
var push = new PushBroker();
//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes("ExamplePushCertificate.p12"));
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "p12 password"));
push.QueueNotification(new AppleNotification()
    .ForDeviceToken("Device Token Reg Id")
    .WithAlert("Hello World!")
    .WithBadge(7)
    .WithSound("sound.caf"));

Run the application and enjoy the magic of Push Notifications.



Enjoy Coding.... :)