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.
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
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");
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");
Try this:
Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);
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.
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...