Copié el archivo mp3 (kalimba.mp3) en la carpeta raw
formato en la carpeta res
. Pero cuando se activa la notificación, produce el sonido predeterminado.
Así es como hago una notificación:
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); }
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
si se define DEFAULT_SOUND, entonces el sonido predeterminado anula cualquier sonido
R.raw.kalimba
es un ID de recurso entero ; quieres el nombre del recurso de sonido en ese Uri
. Así que prueba:
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba");
Prueba esto:
Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd); notification.setSound(sound);
Debes reemplazar esta línea:
notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);
con este:
notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"));
o en algunos casos:
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba");