I'm working on an android app where I need to display notifications to user. The problem is the notification sound is not played. vibration is working fine though. When check the app settings the 'allow sound' option is disabled by default. As I enable it, everything works fine. however i need it to be enabled by default. like this
These two permissions do exists in manifest.
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.VIBRATE" />
and here is the code snippet i use to create the notification channel and display notification.
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, title,NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(alarmSound, attributes);
channel.enableLights(true);
channel.enableVibration(true);
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle(title)
.setSound(alarmSound)
.setAutoCancel(true)
.setVibrate(new long[]{500L,500L,500L})
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentText(stringBuilder.toString().trim()).build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIF_ID, notification);
}