title | description | author | tags | date_published |
---|---|---|---|---|
Grails 3 sending push notification using Firebase Cloud Messaging |
Step by step tutorial on how to send push notification using Grails 3 and Firebase Cloud Messaging. |
didinj |
Firebase, Firebase Cloud Messaging, FCM, Grails, Grails 3, Push Notification |
2017-08-23 |
Contributed by the Google Cloud community. Not official Google documentation.
This tutorial shows how to send push notification using Firebase Cloud Messaging (FCM) and Grails 3 Framework web application.
- Configure Firebase to send push notifications with a web application
- Create a new Grails 3.3.0 web application
- Create function for sending push notification on Grails 3.3.0
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
This tutorial uses Firebase Cloud Messaging service which is free for limited features and capacities.
- Open your browser then go to Firebase.
- Login using your Gmail account then click Go to Console button.
- Click add project button then give it a name. For this tutorial name it "GrailsFCM" then click Create Project button.
- It will redirect to the Project dashboard. Click the gear icon in the left menu and select Project Settings.
- Click the Cloud Messaging tab and copy the Server Key to your notepad. You will use it later in the Grails web application.
- Next, configuration your app for Android or iOS.
After installing JDK 8 and Grails 3.3.0 and updating your environment path, you can create the new Grails 3.3.0 application.
-
Open terminal or command line then go to your projects folder.
-
Run this command:
grails create-app grails-fcm
-
Go to the newly created Grails 3 application folder:
cd grails-fcm
-
Enter Grails 3 interactive console by type this command:
grails
-
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>
-
Test your Grails web application by typing this command in the Grails interactive console:
run-app
To access Firebase using REST API, add the Grails Datastore Client Rest dependency to the Grails 3 application.
-
Open and edit
build.gradle
. -
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" ... }
-
Compile the Grails 3 application by typing this command in the Grails interactive console:
compile
Run the following steps:
-
Create controller by running this command:
create-controller Fcm
-
Add import to the file
grails-app/controllers/grails/fcm/FcmController.groovy
:import grails.plugins.rest.client.RestBuilder
-
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*****'
-
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). -
Run and test push notification from the browser.
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:
- Go to Firebase Console again.
- Choose the project that you want to remove.
- Click gear icon on the left menu then click Project Settings menu.
- Scroll down the you will find delete project button.
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.