Android custom notification sound

When you send a notification to the user of your Android application, you might want to use your own sound. In this Android tip, I am going to show you how to use a custom notification sound.
To achieve this goal, first you need to have a sound file in res/raw folder. In my case, I have kalimba.mp3. If the raw folder does not exist, create it. After that, create a class called MyReceiver that extends the BroadcastReceiver class. The BroadcastReceiver has a method called onReceive to receive an intent object sent from an activity. In this example, the intent object is sent from the MainActivity. As the intent is received, the notification will be sent to the user. So the code to create a notification that uses a custom sound has to be written here. You will use the setSound method of the Notification to specify path of the custom sound.

package com.example.andtip;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
   NotificationManager notificationManager;
   Notification myNotification;
public void onReceive(Context c, Intent i) {
      PendingIntent pi = PendingIntent.getBroadcast(c, 0, new Intent("com.example.andtip"),0 );
      myNotification=new NotificationCompat.Builder(c)
      .setContentTitle("This is a notification that uses a custom sound.")
      notificationManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

In the AndroidManifest.xml file, you need to register the broadcast receiver by adding the <receiver> tag above the closing of the application (</application>).

 <receiver android:name="com.example.andtip.MyReceiver" >
           <action android:name="com.example.andtip" />

In onStart method, the MainActivity class, the code is written to send an intent object to the broadcast receiver when the application launches.

public void onStart(){
   Intent intent=new Intent("com.example.andtip");


Posted by: Dara | post date: 06-28-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.