Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set notification with custom sound in android

Tags:

android

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);
}
like image 683
user1065434 Avatar asked Dec 07 '12 09:12

user1065434


People also ask

Can I set different sounds for Android notifications?

On the Notification category page, scroll down to the Sound section. It shows the default tone enabled for the app. Tap Sound and select your desired notification tone from the list to change the presets.

Can you make custom notification sounds?

Go to the Notifications folder found in Internal Storage and tap Move Here in the bottom right corner. You'll see the custom sound in the notification sound library for you to use for alerts moving forward!


4 Answers

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

like image 168
QuokMoon Avatar answered Oct 04 '22 10:10

QuokMoon


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");
like image 21
dsandler Avatar answered Oct 04 '22 10:10

dsandler


Try this:

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);
like image 40
Dipesh Adhikari Avatar answered Oct 04 '22 11:10

Dipesh Adhikari


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");
like image 27
Noir Avatar answered Oct 04 '22 10:10

Noir