50

I copied the mp3 (kalimba.mp3) file into the raw folder in the res folder. But when the notification is triggered it produces the default sound.

This is how I make a notification:

protected void GenerateNotify() {

    NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  
    Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100);
    Intent intent=new Intent(getApplicationContext(),as.class);
    PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
    notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);
    myNotificationManager.notify(NOTIFICATION_ID,notification);
}
PHPirate
  • 7,023
  • 7
  • 48
  • 84
user1065434
  • 711
  • 1
  • 6
  • 10
  • Your code looks ok. Try rebooting you emulator or phone. http://stackoverflow.com/questions/5682321/android-notification-sound-defaulting-back-instead-of-playing-custom-sound – Aleksander Gralak Dec 07 '12 at 09:31

6 Answers6

118
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

if defined DEFAULT_SOUND, then the default sound overrides any sound

David Passmore
  • 6,089
  • 4
  • 46
  • 70
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
18

R.raw.kalimba is an integer resource ID; you want the name of the sound resource in that Uri. So try:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/kalimba");
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
dsandler
  • 2,471
  • 17
  • 11
5

Try this:

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);
Garg
  • 2,731
  • 2
  • 36
  • 47
3

You should replace this line:

notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);

with this:

notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"));

or in some cases:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba");
Noir
  • 198
  • 1
  • 7
0

I make this static function in HelperClass.java

    public static void givenotification(Context context,String message) {
         NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

         Intent notificationIntent = new Intent(context, DesireActivity.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

         Notification notification = new Notification();
         NotificationCompat.Builder builder;
         builder = new NotificationCompat.Builder(context);

         notification = builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker(context.getString(R.string.app_name)).setWhen(System.currentTimeMillis())
            .setAutoCancel(true).setContentTitle(context.getString(R.string.app_name))
            .setContentText(message).build();
         notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.kalimba);
         notificationManager.notify(1, notification);
     }

Then any Activity like in MainActivity HelperClass.givenotification(MainActivity.this,"Your Notification Message"); OR in fragment HelperClass.givenotification(getActivity(),"Your Notification Message");

Hope this help someone.

Rajkumar Sharma
  • 554
  • 1
  • 4
  • 9
-1

I have implemented this way to set custom Notification sound in my app.

  int notificationID=001;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setSmallIcon(R.drawable.room);
        mBuilder.setContentTitle("Room Rent , Pay Room Rent");
        mBuilder.setContentText("Hi, Its time to pay your Room Rent!");
        mBuilder.setAutoCancel(true);
        mBuilder.setColor(getResources().getColor(R.color.colorViolet));
        mBuilder.setSound(Uri.parse("android.resource://com.roommanagement.app/" + R.raw.notification_sound));

        if (mNotificationManager!=null){
            mNotificationManager.notify(notificationID, mBuilder.build());
        }

Hop this will help you.Thanks...

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30