Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Grails 3 Sending Push Notification using Google Firebase Cloud Messaging description: Step by step tutorial on how to send push notification using Grails 3 and Google Firebase Cloud Messaging. author: didinj tags: Firebase, Firebase Cloud Messaging, FCM, Grails, Grails 3, Push Notification date_published: 2017-08-23


This tutorial shows how to send push notification using Firebase Cloud Messaging (FCM) and Grails 3 Framework Web Application.

Objectives

  • Configure Firebase to send push notification via Web Application
  • Create a new Grails 3.3.0 Web Application
  • Create function for sending push notification on Grails 3.3.0

Before You Begin

The following is required for this tutorial:

  • Terminal or Command Line
  • Text Editor or IDE (Atom, Netbeans)
  • Java Development Kit 8
  • Grails 3.3.0 SDK (https://grails.org/download.html)
  • Google or Gmail account for accessing Firebase dashboard

Costs

This tutorial uses Firebase Cloud Messaging service which is free for limited features and capacities.

Configure Firebase Cloud Messaging

  1. Open your browser then go to Firebase.
  2. Login using your Gmail account then click Go to Console button.
  3. Click add project button then give it a name. For this tutorial name it "GrailsFCM" then click Create Project button.
  4. It will redirect to the Project dashboard. Click the gear icon in the left menu and select Project Settings.
  5. Click the Cloud Messaging tab and copy the Legacy Server Key to your notepad. You will use it later in the Grails web application.
  6. Next, configuration your app for Android or iOS.

Create New Grails 3.3.0 Web Application

After installing JDK 8 and Grails 3.3.0 and updating your environment path, you can create the new Grails 3.3.0 application.

  1. Open terminal or command line then go to your projects folder.
  2. Run this command:

    grails create-app grails-fcm
    
  3. Go to the newly created Grails 3 application folder:

    cd grails-fcm
    
  4. Enter Grails 3 interactive console by type this command:

    grails
    
  5. In the Grails interactive console, you will see the following:

    | Resolving Dependencies. Please wait...
    
    CONFIGURE SUCCESSFUL
    
    | Enter a command name to run. Use TAB for completion:
    grails>
    
  6. Test your Grails web application by typing this command in the Grails interactive console:

    run-app
    

Add Grails Datastore Rest Client Dependency

To access Firebase using REST API, add the Grails Datastore Client Rest dependency to the Grails 3 application.

  1. Open and edit build.gradle.

  2. Add the Grails Datastore Client Rest dependency inside the dependencies section. There are two dependencies, one inside buildscript and the other outside buildscript:

    dependencies {
        ...
        compile "org.hibernate:hibernate-core:5.1.2.Final"
        compile "org.hibernate:hibernate-ehcache:5.1.2.Final"
        compile 'org.grails:grails-datastore-rest-client:5.0.0.RC2'
        console "org.grails:grails-console"
        profile "org.grails.profiles:web"
        runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.11.6"
        ...
    }
    
  3. Compile the Grails 3 application by typing this command in the Grails interactive console:

    compile
    

Create Grails Controller and View for Sending Push Notification

Run the following steps:

  1. Create controller by running this command:

    create-controller Fcm
    
  2. Add import to the file grails-app/controllers/grails/fcm/FcmController.groovy:

    import grails.plugins.rest.client.RestBuilder
    
  3. Create a method to FcmController, so it will looks like this.

    package grails.fcm
    
    import grails.plugins.rest.client.RestBuilder
    
    class FcmController {
    
      def index() { }
    
      def sendPushNotification() {
        def regid = params.regid
        def title = params.title
        def body = params.body
    
        def rest = new RestBuilder(connectTimeout:1000, readTimeout:20000)
        def resp = rest.post("https://fcm.googleapis.com/fcm/send") {
          header 'Content-Type', 'application/json'
          header 'Authorization', 'key=AIza*****'
          json {
            notification = {
              title = title
              body = body
              sound = "default"
              click_action = "FCM_PLUGIN_ACTIVITY"
              icon = "fcm_push_icon"
            }
            to = regid
          }
        }
    
        flash.message = "Notification sent"
        redirect action: "index"
      }
    }
    

    Enter the Legacy Server key you saved from your project dashboard in the line that begins with 'key=':

    header 'Authorization', 'key=AIza*****'
    
  4. Create a view file grails-app/views/fcm/index.gsp with the content from [index.gsp]((https://github.com/GoogleCloudPlatform/community/blob/master/tutorials/grails3-send-push-notification-using-firebase-cloud-messaging/index.gsp).

  5. Run and test push notification from the browser.

Cleaning Up

After you have finished this tutorial, clean up your Firebase project because you can only have 3 projects in the free tier. Just do the following:

  1. Go to Firebase Console again.
  2. Choose the project that you want to remove.
  3. Click gear icon on the left menu then click Project Settings menu.
  4. Scroll down the you will find delete project button.

Learn More

Visit Grails Guides to learn more on official Grails guides or if you want different tutorial style you can find here.

View the source code for this tutorial on GitHub.

See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.