How to add sound to notification?

How do you add sound to a notification created by NotificationCompat.Builder? I created a raw folder in res and added the sound there. So how do I now add it to notification? This is my Notification code

    int NOTIFY_ID=100;
    Intent notificationIntent = new Intent(this, Notification.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("Warning")
            .setContentText("Help!")

    NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mgr.notify(NOTIFY_ID, mBuilder.build());

I'm guessing the problem here is how to reference the sound with a Uri, as there is an obvious method in the NotificationCompat.Builder class - setSound(Uri soundUri).

To access your raw resources you need to create the Uri as follows:

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

So the code could end up looking like that:

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
mBuilder.setSound(sound);

How to Add Notification Sounds on Android: 10 Steps, This wikiHow teaches you how to set any sound file as your notification sound, using Android. Copy a sound file to your Android device. Click to Expand. 1 Head into the My Files app. 2 Tap on Audio. 3 Select Music > long press the music file to select then either tap on Move or Copy. 4 Head back to the My File main page and select Internal storage. 5 Select Notifications. 6 Tap on Copy here to copy your music file into your Notifications folder.

To play a sound with your notification:

Notification notification = new Notification(icon, tickerText, when);

Do normal notification procedures

To play the default sound with your notification:

notification.defaults |= Notification.DEFAULT_SOUND;

To play a custom sound with your notification:

notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3");

Then just use the notification manager to send the notification. If both of these statements are used, the application will default to using the default sound.

How to add sound to notification?, I'm guessing the problem here is how to reference the sound with a Uri , as there is an obvious method in the NotificationCompat.Builder class� Quick tip: As part of the notifications sounds, you can also change the sounds for other notifications, including “New Mail Notification,” “New Text Message Notification,” “Desktop Mail Notification,” “Calendar Reminder,” and “Instant Message Notification.” Click the Apply button. Click the OK button.

For Android 8 (Oreo) and above you should use Notification Channel to show notifications, and they will automatically have sound if you haven't disabled it.

For older devices you can set sound with Notification Builder. You can use Notification.Builder.setSound() to set custom sound, or you can use

Notification.Builder.setDefaults(NotificationCompat.DEFAULT_SOUND) to set default sound.

There are also defaults for vibration and lights

How to set MP3 file custom ringtone & notification sound on Android, You add your own MP3 to the list, of course. In this guide, I'll show you how to set an MP3 file as custom ringtone or notification sound on certain high-end� To play a custom sound with your notification: notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3"); Then just use the notification manager to send the notification. If both of these statements are used, the application will default to using the default sound. share.

Custom Notification Sounds - The Proper Way!, In today's video, we will cover how to properly set custom notification sounds. Custom Duration: 5:08 Posted: Jan 28, 2020 Copy your file there. Copied file. When you’ve done so, open your Settings app. Tap the Sounds and Vibration option, and then the Notification sound option. Sounds and vibration. Here, you can now select your notification options – if you have a dual SIM model, you can set custom tunes for each SIM separately.

How to Add Custom Notification Sounds on Android Phone, Got some songs or sounds you want to use as notification? Simply follow the steps here and Duration: 2:34 Posted: Mar 23, 2019 Select the email address associated with the account for which you want to modify notifications. Do any of the following: Check “Email notifications” to enable notifications or uncheck it to disable all notifications. Select “Choose ringtone” to change the sound that plays when an email arrives.

Custom Notification Sound | Samsung Galaxy, Quick video demo showing how to change the notification sound for text messaging for Duration: 2:23 Posted: Apr 4, 2016 ok I have figured it out. Start in settings. Go to notifications (photo) or apps. Go to preferred app. Then go to "Device Level Notifications " and in there you will see the spot to change to sound for the app.

Comments
  • There's a setSound method in NotificationCompat.Builder. Is that what you're looking for?
  • What format does my sound file need to be in? I'm getting a default sound instead of my own.
  • I've answered my own question with trial and error: 1) mp3 works, wav doesn't, and 2) I need to disable the default sound by building the notification and then doing n.defaults &= ~Notification.DEFAULT_SOUND;
  • If you are using Notification.Builder, builder.setDefaults(~Notification.DEFAULT_SOUND); will do the trick. Thanks to @karl for the tip
  • default sound doesn't work on Android 4-7 for me with NotificationCompat stackoverflow.com/questions/56407328/…
  • default sound doesn't work on Android 4-7 for me with NotificationCompat stackoverflow.com/questions/56407328/…