Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sound to notification?

Tags:

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()); 
like image 949
Vladimir Avatar asked Dec 26 '12 17:12

Vladimir


People also ask

Can you make your own audio as a notification sound?

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.


1 Answers

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); 
like image 109
Paul Avatar answered Oct 13 '22 02:10

Paul