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());
How to Make a Custom Notification Sound on Mobile On Android? Android provides various default notification sounds. Yet, you can use the Audio Files as Custom Notifications Sound. It requires you to move or copy the audio file to the Notifications folder and select it from the Settings.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With