0

In the following code I create a notification and display it with vibration and sound. As it is now it works perfectly fine.

However, if I change to using sounds from the RingtoneManager, as I have seen multiple other examples do, no sound gets played (no error messages, just no sound). Does anyone know how I can get those sounds to play?

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setStyle(new NotificationCompat.InboxStyle());
builder.setPriority(NotificationCompat.PRIORITY_HIGH);

Uri alarmsound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification); // Works fine
// Uri alarmsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Doesn't work

builder.setSound(alarmsound);
long[] pattern = {1000, 500, 500, 1000};
builder.setVibrate(pattern);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(1, notification);
Androidas
  • 169
  • 10
  • check this, http://stackoverflow.com/questions/4441334/how-to-play-an-android-notification-sound – Silvans Solanki Mar 21 '16 at 13:13
  • 1
    @SilvansSolanki: Thanks! I had looked at that one already, but a second glance made me try other types as well, and it seems like it is only `TYPE_NOTIFICATION` that doesn't work, while `TYPE_ALARM` and `TYPE_RINGTONE` play just fine. – Androidas Mar 21 '16 at 13:28

0 Answers0