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);