Push Notification in IOS Using Grails

31 / Jul / 2015 by madhav.khanna 0 comments

Hi All,

In my previous blog . I have explained how to send the push notification to Android Device. Now, it’s time to see how we can send push notification to iOS devices.

For sending Push Notification in iOS I have used JavaPNS_2.2.jar .

So before, directly jumping on example we should know that what are prerequisites of sending push notification to iOS device.

  1. We need to create an application in iPhone Developer Connection Portal. After creating it we need to create a push cretificate from it which is a .p12 file which will be used for sending the push notification to APNS.
  2. Then we need to install iOS app on our device which will sends sender ID and application ID to APNS server for registration (Generally Done by iOS Developer).
  3. Upon successful registration APNS server issues registration ID to iOS device.
  4. After receiving registration ID, the device will send the registration ID to our server.
  5. Our server will store the registration ID in the database for later usage.
  6. Whenever a push notification is needed, our server sends a message to APNS server along with the device registration ID (which has been stored earlier in the database).
  7. After sending the push notification to the APNS server then it is the responsibilty of APNS server to send the push notification to the desired device using registration ID.

IOS Push

So now, in order to use the JavaPNS_2.2 jar we must copy it in lib directory of grials-app and we can also mention the path of our push certificate file in our Config.groovy for easier access.

 ios.certificate.location = "${userHome}/PushCertificates.p12" 

How to send push notification

  1. For sending the push notification to iOS device we first need to create the Payload by using the PushNotificationPayload class. This class provide us with different methods to modify the payload but in my case I have used only two of them i.e.

    • addAlert(String alertMessage)
    • addCustomDictionary(String name,String value)
  2. After create the Payload we can send push notification to iOS device by using the static payload() method of Push class.

Let’s take an example.

BirthdayReminderJob

class BirthdayReminderJob {

    def userService
    def applePushNotificationService
    

    static triggers = {
        cron name: 'birthdayReminder', cronExpression: "0 20 5 1/1 * ? *"
    }

    def execute() {
         List<User> birthdayUserList = userService.birthdayList()
         if (birthdayUserList) {
            applePushNotificationService.sendPushNotification(birthdayUserList)
        }
    }
}

ApplePushNotificationService

class ApplePushNotificationService {


    def grailsApplication
    def mobileAppUserService
    def mobileUtilService

    void sendPushNotification(List birthdayUserList) {
        List tokenList = mobileAppUserService.getTokenList(MobilePlatform.IOS)
        Map birthDayMap = mobileUtilService.getNotification(birthdayUserList,tokenList)
        if (birthDayMap) {
            sendPushNotificationToIOSUser(birthDayMap, tokenList)
        }
    }

    def sendPushNotificationToIOSUser(Map birthDayMap, List tokenList) {
        PushNotificationPayload payload = PushNotificationPayload.complex()
        payload.addAlert(birthDayMap.title)
        payload.addCustomDictionary("birthDayMap", birthDayMap.isBirthday)
        String password = ""
	String filePath = grailsApplication.config.ios.certificate.location

        tokenList.each { String token ->
            try {
               PushedNotifications pushedNotifications = Push.payload(payload, filePath, password, false, token)
            } catch (Exception exception) {
                log.info(exception)
            }
        }
    }

}

Hope this helps !! :) :)

FOUND THIS USEFUL? SHARE IT

Leave a comment -