{"id":24595,"date":"2015-07-31T11:19:13","date_gmt":"2015-07-31T05:49:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24595"},"modified":"2015-07-31T11:19:13","modified_gmt":"2015-07-31T05:49:13","slug":"push-notification-in-ios-using-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/push-notification-in-ios-using-grails\/","title":{"rendered":"Push Notification in IOS Using Grails"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>In my <a href=\"http:\/\/www.tothenew.com\/blog\/push-notification-in-android-using-grails\/\"> previous blog <\/a>. I have explained how to send the push notification to Android Device. Now, it&#8217;s time to see how we can send push notification to iOS devices.<\/p>\n<p>For sending Push Notification in iOS I have used <strong> JavaPNS_2.2.jar <\/strong>.<\/p>\n<p>So before, directly jumping on example we should know that what are prerequisites of sending push notification to iOS device.<\/p>\n<ol>\n<li> 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.<\/li>\n<li> 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).<\/li>\n<li> Upon successful registration APNS server issues registration ID to iOS device.<\/li>\n<li> After receiving registration ID, the device will send the registration ID to our server.<\/li>\n<li> Our server will store the registration ID in the database for later usage.<\/li>\n<li> 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).<\/li>\n<li> 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.<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/IOS-Push.jpg\" alt=\"IOS Push\" width=\"467\" height=\"500\" class=\"alignnone size-full wp-image-24597\" \/><\/p>\n<p>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 <strong>Config.groovy<\/strong> for easier access.<\/p>\n<p>[java] ios.certificate.location = &quot;${userHome}\/PushCertificates.p12&quot; [\/java]<\/p>\n<p><strong>How to send push notification <\/strong><\/p>\n<ol>\n<li>\nFor 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.<\/p>\n<ul>\n<li>addAlert(String alertMessage)<\/li>\n<li>addCustomDictionary(String name,String value)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>After create the Payload we can send push notification to iOS device by using the static payload() method of Push class.\t<\/p>\n<\/li>\n<\/ol>\n<p><strong>Let\u2019s take an example.<\/strong><\/p>\n<p><span style=\"text-decoration: underline\">BirthdayReminderJob<\/span><\/p>\n<p>[java]<br \/>\nclass BirthdayReminderJob {<\/p>\n<p>    def userService<br \/>\n    def applePushNotificationService<\/p>\n<p>    static triggers = {<br \/>\n        cron name: &#8216;birthdayReminder&#8217;, cronExpression: &quot;0 20 5 1\/1 * ? *&quot;<br \/>\n    }<\/p>\n<p>    def execute() {<br \/>\n         List&lt;User&gt; birthdayUserList = userService.birthdayList()<br \/>\n         if (birthdayUserList) {<br \/>\n            applePushNotificationService.sendPushNotification(birthdayUserList)<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><span style=\"text-decoration: underline\">ApplePushNotificationService<\/span><\/p>\n<p>[java]<br \/>\nclass ApplePushNotificationService {<\/p>\n<p>    def grailsApplication<br \/>\n    def mobileAppUserService<br \/>\n    def mobileUtilService<\/p>\n<p>    void sendPushNotification(List birthdayUserList) {<br \/>\n        List tokenList = mobileAppUserService.getTokenList(MobilePlatform.IOS)<br \/>\n        Map birthDayMap = mobileUtilService.getNotification(birthdayUserList,tokenList)<br \/>\n        if (birthDayMap) {<br \/>\n            sendPushNotificationToIOSUser(birthDayMap, tokenList)<br \/>\n        }<br \/>\n    }<\/p>\n<p>    def sendPushNotificationToIOSUser(Map birthDayMap, List tokenList) {<br \/>\n        PushNotificationPayload payload = PushNotificationPayload.complex()<br \/>\n        payload.addAlert(birthDayMap.title)<br \/>\n        payload.addCustomDictionary(&quot;birthDayMap&quot;, birthDayMap.isBirthday)<br \/>\n        String password = &quot;&quot;<br \/>\n\tString filePath = grailsApplication.config.ios.certificate.location<\/p>\n<p>        tokenList.each { String token -&gt;<br \/>\n            try {<br \/>\n               PushedNotifications pushedNotifications = Push.payload(payload, filePath, password, false, token)<br \/>\n            } catch (Exception exception) {<br \/>\n                log.info(exception)<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>Hope this helps !! \ud83d\ude42 \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, In my previous blog . I have explained how to send the push notification to Android Device. Now, it&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":96,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7,1400,446,1],"tags":[2083,4840,4848,4844,2078],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24595"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/96"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24595"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24595\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}