Complete Guide on How to Configure Your iOS App For Push Notifications

0
Shares
Complete Guide on How to Configure Your iOS App For Push Notifications
5 (100%) 2 votes

Imagine, you’ve created an iOS app and its downloads have sky-rocketed in App Store, users are using your app all day, every day, and you’re on the cloud 9. But, unfortunately, users have to close your app to execute other activities. Laundry doesn’t fold by itself you know.

However, through Apple iOS push notification service, you can re-engage your app users.

Over time, push notifications iOS have become more and more powerful after they were first introduced. Currently, push notifications can:

  • Play a notification sound
  • Display a short text message
  • Set a badge number on the app’s icon
  • Offers to perform some certain actions without opening the app
  • Allows App to work in the background and perform a task

In case, you want to enable push notification for your app, send this simple step by step guide to your developers.

This complete guide will cover how the push notifications work, but before you get started, you’ll need the following to test iOS push notification tutorial.

An iOS Device: Push notifications do not run in test simulator, so you’ll require an actual device.

An Apple Developer Program Membership: after Xcode 7, you can now test your apps without a program membership, but to configure push notifications, a push notification certificate is required for your app ID, which ultimately requires the program membership.

Let’s Get Started…

There are basically three important tasks that need to be performed to enable push notifications feature.

1. You have to configure your app properly and register it with the Apple Push Notification Service (APNS) to execute push notifications.

2. A server must send the push notifications to APNS directed to one or more devices.

3. Your app must receive the push notification so that it can handle actions and perform certain tasks using callbacks in the application delegate.

Configuring Your App For Push Notifications

Push notifications need security because you don’t want anyone to send push notifications to your app users rights? Therefore, there are a couple of hoops that you need to jump in order to configure your app for push notifications.

There are two types of notifications: Local and Push (or Remote).
For the local notification, you can manage it from your device. In this tutorial, we will describe how Remote Push notification works.

All the push notifications follow a mandatory path with go through the Apple Push Notification Servers, usually, we call it APNS.

To enable our app for push notification we have to follow certain steps with are given below

First of all, we need an “Apple Developer Account” to manage push notification enable for our app.

Now we are going to get CSR(Certificate Signing Request) and for that follow given below steps

1. Open “Keychain Access”

2. Click on “Keychain Access” on the menu bar

3. Then go to “Certificate Assistant” —> “Request a Certificate From a Certificate Authority”

Notification1

 

Notification3

It will open a popup in which enter your Email and Name and check the radio button “Saved to disk” and click on “Continue”.
Now you can see “CertificateSigningRequest.certSigningRequest” file on your desktop

Now login to your Apple developer account. Click on Certificate, Identifier & Profile option.

Notification4

Then Go to “App IDs” and Add (+ icon on top right) new app id in it.

Notification5

It will ask you Name of App ID, App ID Prefix(don’t change that) and Bundle ID (it should be same in your XCODE project)

When you create Bundle ID then App ID should be unique for Game Center, In-App-Purchase, Data Protection, and Push Notification services(wildcard App ID with and asterisk * is not allowed in such cases).

Also, you can see an option like:

Notification6

Select push notification option from it. In case if you are not enabling push notification at that moment then you can do it later on.

When you click on App ID and click on it. You can see Push Notification in the list like given below.

Notification7

Click on “Edit” button will provide you access to do the changes in App ID regarding “Push Notification”

Mark on “Push Notification” will make “Development SSL Certificate” and “Production SSL certificate’s” enable and you can click on “Create Certificate” options.

Notification8

Click on “Create Certificate” If will ask for create CSR (which we have already created first and saved on desktop) and give yo all the steps to create CSR.

Notification9

Click on “Continue” and go to next screen
click to the Choose File… and select “CertificateSigningRequest.certSigningRequest”

Notification10

Finally, click on “Generate” and you will get a certificate of PUSH NOTIFICATION. it will have .cer extension.

Notification12

 

Now for install it, please double click on it. It will be shown in your “Keychain Access”
Then Right click on that, it will provide you option for “Export Apple Development IOS Push……..”
Export certificate, it will be in .p12 format and then save it.

You can skip password setting when you save a certificate in .p12 format.

Not you can convert .p12 in .pem file and it will be used on server side.

Now Code in XCODE

1. Write following code in “didFinishLaunchingWithOptions” for register device for allows push notification

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if application.respondsToSelector(#selector(UIApplication.registerUserNotificationSettings(_:))) {
let types :UIUserNotificationType = [.Alert, .Badge, .Sound]
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}else {
// Register for Push Notifications before iOS 8
let settings = UIRemoteNotificationType.Alert.union(UIRemoteNotificationType.Badge).union(UIRemoteNotificationType.Sound)
application.registerForRemoteNotificationTypes(settings)
}

return true
}

When didFinishLaunchingWithOptions will be called for the first time then a popup should be shown with “Allow” and “Not now”.

If a user clicks on “Allow” we get device token and if a user clicks on “Not now” then it will go to Fail registration delegate method, now below we discuss delegate of remote push notification.

2. Next, we have to write delegate of Remote Notification

When user Allow push notification then given below notification will be called

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("DEVICE TOKEN = \(deviceToken)")
}

Once get device token successfully it will sure that we are on the right way.

Else it will be

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error)
}

When a notification comes from server we can handle it from

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}

This delegate method offers an opportunity for applications with the “remote-notification” background mode to fetch appropriate new data in response to an incoming remote notification. You should call the fetchCompletionHandler as soon as you’re finished performing that operation, so the system can accurately estimate its power and data cost.

This method will be invoked even if the application was launched or resumed because of the remote notification. The respective delegate methods will be invoked first. Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented.

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print(userInfo)
if application.applicationState == .Inactive {

} else if (application.applicationState == .Background) {

} else if (application.applicationState == .Active){

}
}

So, there you go! By following the above steps, you can now easily configure your IOS application for push notifications. Also, the code for the same is available on the GitHub here.

The following image is a sample of a push notification.

Sample Push Notification

If you still have any kind of queries or doubts, you can contact anyone from our IOS development team and you can also Hire iPhone App Developer from us. We have a team of experienced professionals who have developed more than 500 Mobile apps. You can check our portfolio here.

 
0
Shares
 

Have an App Idea?

Get your free consultation now