Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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

Steps

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.

<dependency>

            <groupId>com.ganyo</groupId>

            <artifactId>gcm-server</artifactId>

            <version>1.0.2</version>

            <type>jar</type>

 </dependency> 

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

            Logger.getLogger(SendPushNotification.class.getName())

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

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
java

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}