Push Notification in Android Using Grails

31 / Jul / 2015 by madhav.khanna 1 comments

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 events delivered by a centralized server to an endpoint device even when the user is not actively using your application.

How it Works :

  1. We need to create an application in Google Play Developer console , after creating the application it will provide us with a key through which our application will send the notification to GCM server.
  2. 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).
  3. Upon successful registration GCM server issues registration ID to Android 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 GCM server along with the device registration ID (which has been stored earlier in the database).
  7. 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.

Android Push

Let’s start by taking an example.

In order to use the plugin we must include in BuildConfig.groovy

[java]compile ":android-gcm:0.2"[/java]

and we can also keep GCM key in Config.groovy

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).


class BirthdayReminderJob {

def userService
def googleCloudMessagingService

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

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


class GoogleCloudMessagingService {

def androidGcmService
def mobileAppUserService
def grailsApplication
def mobileUtilService

void sendPushNotification(List<User> birthdayList) {
List<String> tokenList = mobileAppUserService.getTokenList(MobilePlatform.ANDROID)
Map birthDayMap = mobileUtilService.getNotification(birthdayList, tokenList)
if (birthDayMap) {
Map response = ["response": JsonOutput.toJson(birthDayMap)]
sendPushNotificationToAndroidUser(response, tokenList)

void sendPushNotificationToAndroidUser(Map response, List<String> tokenList) {
sendMulticastInstantMessage(response, tokenList)

void sendMulticastInstantMessage(Map response, List<String> registrationIds, String apiKey = apiKeyFromConfig()) {
androidGcmService.sendMessage(response, registrationIds, ”, apiKey)

String apiKeyFromConfig() {
return grailsApplication.config.android.gcm.api.key


Hope this helps !! πŸ™‚ πŸ™‚


comments (1 “Push Notification in Android Using Grails”)

  1. Narendra

    Hi madhav, it’s really help full blog for me , i have a stupid question , can you please tell me mobileUtilService.getNotification(birthdayList, tokenList) returns ?


Leave a Reply

Your email address will not be published. Required fields are marked *