Android Notification with Sound and Icon Tutorial

My app needed a simple Android notification with sound and icon. So here’s the code I used to make that happen. I know that there are many other types of notification in Android, but this time, I just want to show you a very simple code that can be a solution to your problem too!

I will probably create a series of blog posts regarding Android notifications, and make it like a cheat sheet for all of us. Anyway, here are some screenshots of today’s code output:

When you run this code.


When you click the button “Show Notification”,
the notification bar will have the ninja icon
and you will hear a sound
(make sure you’re not in silent mode).


When you slide down the notification bar.


Download Code

Hide Notification

How to hide the notification? There are two ways:

1. First, you can click the “Cancel Notification” button.

2. Or, second, swipe the notification to left or right (swipe the notification in the third image).

These ways were set programatically, so read the code (with comments) below.

Let’s Code!

Here’s are our awesome code:

package com.example.androidnotificationbar;

import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {


        // listener handler
        View.OnClickListener handler = new View.OnClickListener(){
            public void onClick(View v) {

                switch (v.getId()) {



        // we will set the listeners


    public void showNotification(){

        // define sound URI, the sound to be played when there's a notification
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        // intent triggered, you can add other intent for other actions
        Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
        PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

        // this is it, we'll build the notification!
        // in the addAction method, if you don't want any icon, just set the first param to 0
        Notification mNotification = new Notification.Builder(this)

            .setContentTitle("New Post!")
            .setContentText("Here's an awesome update for you!")

            .addAction(, "View", pIntent)
            .addAction(0, "Remind", pIntent)


        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // If you want to hide the notification after it was selected, do the code below
        // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, mNotification);

    public void cancelNotification(int notificationId){

        if (Context.NOTIFICATION_SERVICE!=null) {
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);


I also put the ninja icon in our drawable folder, that’s the source of our icon.

Please share what Android notification example you want next!


Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

9 Responses to "Android Notification with Sound and Icon Tutorial"

  1. Pablo ungaro says:

    Post one example for c2dm please

  2. Hi guys, for those having problems with the code download, see my original post for the fixed download link

  3. ThemeBowl says:

    Hi,Thanks for sharing this tutorial.I would like to know how to show notification on an app after 2 days since its use?

  4. dhara says:

    do u have to set any permission in the manifest.xml file???plz reply

  5. ashish says:

    Hi thanks but I want to show notification after every 24 hours automatically how do I do this please help

  6. Anuj says:

    I am creating this Notification in a Fragment and is showing an error while calling constructor of intent.

    The constructor Intent(QuoteFragment, Class) is undefined

  7. fda says:

    I cannot access your source code link.

  8. Hello all,

    We have fixed the download link, sorry for the inconvenience.

    Best regards

Leave a Reply

three + 5 =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: