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:


the notification bar will have the ninja icon
and you will hear a sound
(make sure you’re not in silent mode).

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: MainActivity.java
package com.example.androidnotificationbar; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // listener handler View.OnClickListener handler = new View.OnClickListener(){ public void onClick(View v) { switch (v.getId()) { case R.id.btnShowNotification: showNotification(); break; case R.id.btnCancelNotification: cancelNotification(0); break; } } }; // we will set the listeners findViewById(R.id.btnShowNotification).setOnClickListener(handler); findViewById(R.id.btnCancelNotification).setOnClickListener(handler); } 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!") .setSmallIcon(R.drawable.ninja) .setContentIntent(pIntent) .setSound(soundUri) .addAction(R.drawable.ninja, "View", pIntent) .addAction(0, "Remind", pIntent) .build(); 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); nMgr.cancel(notificationId); } } }
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!
Thanks for reading this Android Notification with Sound and Icon Tutorial!
13 responses to “Android Notification with Sound and Icon Tutorial”
thank you for the code..but i am getting error while running on my htc sensation device(4.0.3)…getting following errors :: ” java.lang.NoSuchMethodError: android.app.Notification$Builder.addAction “
Hi @disqus_kxk3xqrqVl:disqus, thanks for dropping by! Regarding your problem, are you sure you have the latest Android SDK installed?
Thank u so much for the tutorial.Its working perfect but i am not getting colored icons in Notification . Can u plz tell me how to get colored icons?
Hello Surya, do you see any error messages in your logcat?
@disqus_kxk3xqrqVl:disqus Hi what worked for me was to change line 57 to:
Notification mNotification = new NotificationCompat.Builder(this)
This wil make it work correctly on most API versions of Android.
Hi @personxx:disqus, thanks for your contribution to this post! This tip will be helpful to others too. :)
can you teach as how to get notification sending it from web as a post and getting it as a notification on android app?
Hey @Churva, I think what you need is google cloud messaging?
i tried this app and import it correctly it runs perfectly excepts, there’s no ringing sound.
what is the problem?
Thank you for this code. I tested it on my HTC One S and it works.
But regarding to the two actions that are shown on the notification, are they working also in Android 4.1.1? Because “View” and “Reminds” buttons don’t appear on my phone.
And also I’ve tried to start an intent for the “View” button and a different intent for the “Remind” button, but when I click on the notification (the buttons don’t appear) it opens directly the second intent.
Thank you in advance for your answers!
the View and Reminds buttons..are not shown
Hello,
Am not able to download from the link provided
error appears on the line of create intent in NotificationReceiver.class