{"id":24556,"date":"2015-07-31T11:12:33","date_gmt":"2015-07-31T05:42:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24556"},"modified":"2015-07-31T11:12:33","modified_gmt":"2015-07-31T05:42:33","slug":"push-notification-in-android-using-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/push-notification-in-android-using-grails\/","title":{"rendered":"Push Notification in Android Using Grails"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>I had a use case where I need to send the push notification to Android Device using Grails Application.<\/p>\n<p>For sending push notification I have used the <strong>Android GCM<\/strong> plugin.<\/p>\n<p>So, before going further we should know what is push notification and how it works.<\/p>\n<p><strong>Push Notification <\/strong> : It is a message or events delivered by a centralized server to an endpoint device even when the user is not actively using your application.<\/p>\n<p><strong>How it Works <\/strong> :<\/p>\n<ol>\n<li> We need to create an application in <strong>Google Play Developer console<\/strong> , after creating the application it will provide us with a key through which our application will send the notification to GCM server.<\/li>\n<li> Then we need to install ANDROID app on our device which will sends sender ID and application ID to GCM server for registration(Generally done by Android Developer).<\/li>\n<li> Upon successful registration GCM server issues registration ID to Android 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 GCM 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 GCM server then it is the responsibilty of GCM 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\/Android-Push.jpg\" alt=\"Android Push\" width=\"600\" height=\"430\" class=\"alignnone size-full wp-image-24580\" \/><\/p>\n<p><strong>Let\u2019s start by taking an example.<\/strong><\/p>\n<p>In order to use the plugin we must include in BuildConfig.groovy<\/p>\n<p>[java]compile &quot;:android-gcm:0.2&quot;[\/java]<\/p>\n<p>and we can also keep GCM key in Config.groovy<\/p>\n<p>For sending push notification we have to use androidGcmService provided by the plugin and it gives us different methods for sending push notification, in my case I have used sendMessage(Map data, List registrationIds, String collapseKey, String apiKey).<\/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 googleCloudMessagingService<\/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            googleCloudMessagingService.sendPushNotification(birthdayUserList)<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><span style=\"text-decoration: underline\">GoogleCloudMessagingService<\/span><\/p>\n<p>[java]<br \/>\nclass GoogleCloudMessagingService {<\/p>\n<p>    def androidGcmService<br \/>\n    def mobileAppUserService<br \/>\n    def grailsApplication<br \/>\n    def mobileUtilService<\/p>\n<p>    void sendPushNotification(List&lt;User&gt; birthdayList) {<br \/>\n        List&lt;String&gt; tokenList = mobileAppUserService.getTokenList(MobilePlatform.ANDROID)<br \/>\n        Map birthDayMap = mobileUtilService.getNotification(birthdayList, tokenList)<br \/>\n        if (birthDayMap) {<br \/>\n            Map response = [&quot;response&quot;: JsonOutput.toJson(birthDayMap)]<br \/>\n            sendPushNotificationToAndroidUser(response, tokenList)<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void sendPushNotificationToAndroidUser(Map response, List&lt;String&gt; tokenList) {<br \/>\n        sendMulticastInstantMessage(response, tokenList)<br \/>\n    }<\/p>\n<p>    void sendMulticastInstantMessage(Map response, List&lt;String&gt; registrationIds, String apiKey = apiKeyFromConfig()) {<br \/>\n        androidGcmService.sendMessage(response, registrationIds, &#8221;, apiKey)<br \/>\n    }<\/p>\n<p>    String apiKeyFromConfig() {<br \/>\n        return grailsApplication.config.android.gcm.api.key<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Hope this helps !! \ud83d\ude42 \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, I had a use case where I need to send the push notification to Android Device using Grails Application. For sending push notification I have used the Android GCM plugin. So, before going further we should know what is push notification and how it works. Push Notification : It is a message or [&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":7},"categories":[518,7,1],"tags":[4845,2084,4840,4844,2078],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24556"}],"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=24556"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24556\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}