Send Push Notification to Android Device

DZone 's Guide to

Send Push Notification to Android Device

Want to learn how to send push notifications to Android devices? Check out this quick tutorial on Android push notifications.

· Mobile Zone ·
Free Resource

Often you may need to write services for a mobile app. We all know apps throw push notifications, and push notifications bring us the important information about the app.

    How push notifications are sent to a user:Image title


1. First we fetch the message from the database to our application server. 

2. Send a message to the Google GCM server.

3. Then the GCM server will push those messages whenever the mobile is 

     connected to the internet.

1. We all know how to fetch the data from the database.

2. Sending a message to GCM server

a) First we need to add a gcm-server dependency to our pom.xml file.







b) Create a class and copy the below code. 

import com.google.android.gcm.server.Message;

import com.google.android.gcm.server.Result;

import com.google.android.gcm.server.Sender;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

public class SendPushNotification {

    // google server will profde this key , 

    //when our app register for the push notification

    private static final String GOOGLE_SERVER_KEY= "AIzaSyDDuLlpT4xH";

    // the registration id, that is unique for the each android device

    private static final String regId = "APA91bHmjeWYjOROcG6oEt";

    public static void main(String[] args) {

        try {

            // message that we need to send 

            String userMessage = "message";

            // Register the sender

            Sender sender = new Sender(GOOGLE_SERVER_KEY);

            // prepate the message

            Message message = new Message.Builder().timeToLive(30)

                .delayWhileIdle(true).addData("message", userMessage).build();

            // send the message to the GCM server.

            Result result = sender.send(message, regId, 1);

            System.out.println("push status " + result.toString());

        } catch (IOException ex) {


                    .log(Level.SEVERE, null, ex);




3. There you go, you have successfully submitted the push message to the GCM server. Next time your app client connect to the internet  you message will be delivered by the GCM server.


Published at DZone with permission of SivaTeja Peram . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}