How to send to push notification at apple device using APNS

Posted By : Tushar Paliwal | 21-Jun-2015

Hi there, in this tutorial I'm gonna describe you how can we send push notification at apple devices. One of the widely used feature of iOS device is push notification which allows developers to send directly message to devices in which relavent application is installed.

  • Introduction

Apple actually introduced push notifications to active applications to respond to events if application isn't running in forground mode. Even IOS devices has made few enhancement to perform tasks in background. Push notifications are great to notify application about important events and keep user engaged.

  • Integration of push notification in grails
  • Step 1 :

To start with push notification, we firstly need push notification certification which is provided by client side developers. The push certificate is nothing just a .p12 or .pem file which is basically installed at server machines.

  • Step 2 :

In this tutorial i'll use .jar file for sending push notification, in grails this can be accomplished by using grails plugin. Here i'll use below jar file.

 apns-0.1.6.jar
  • Step 3 :

To send notification we need to initialize the APNS service with appropriate parameters. The below code can be used for development enviornment.

ApnsService apnsService = APNS.newService()
.withCert(CertificatePath,CertificatePassword)
.withSandboxDestination()
.build();
apnsService.start();

To make above code working at production enviornment we only need to replace withSandboxDestination() method with withProductionDestination() which is used when enviornemt is changed to production.

In above code the following terms are :

CertificatePath : Absolute path of push notification certificate which need to be installed at server machine.
CertificatePassword : This is password of push certificate which is being set while creation of certificate.

  • Step 4 :

Now to send the message and other stuff like parameter and sounds, we need to create payload for push notification.

PayloadBuilder payloadBuilder = APNS.newPayload();

PayloadBuilder class has many methods, few of them are as follows :
alertBody() : Method is used to set message which is being shown as push message.
CustomField() : Method is used to send parameters in push notification, which is send in key, value pair.
Sound() : This is basically used to set sound parameter in push notification.
IsTooLong() : This method is used to check whether size of payload is long or not.
ShrinkBody() : This method is used to shrink size of payload to appropriate limit so that it could be send over the network.

  • Step 5 :

Finally we need a device token which is used to send notification at device and could be provided by client.

apnsService.push(deviceToken, payload);

deviceToken : Device token is basically a unique identifier for device to which push is being send.
Payload : Payload is basically created to send over the network for a device.

 

I hope this will be meaningful for you, feel free to ask any query.

 

Thanks

 

About Author

Author Image
Tushar Paliwal

Tushar is a developer with experience in Groovy and Grails , Spring and enterprise Java Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..