Android soundtrack not working

I have tried many things suggested here, but nothing works for me. here is my source code:

Intent resultIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("title") .setContentText("Hello World!"); mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); mBuilder.setContentIntent(contentIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); 

A notification appears, but no sound, any help?

+6
source share
5 answers

Set the default flags:

 mBuilder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE); 
+17
source

I see nothing wrong with how you tune the sound. Another alternative way is to set the sound as follows. Let him go and see if that helps ...

 Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("title") .setContentText("Hello World!") .setSound(alertSound); 

Update
Try editing NotificationManager as follows and try ...

 NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = mBuilder.getNotification(); notification.flags = Notification.FLAG_AUTO_CANCEL; // cancel the notification when clicked by the user mNotificationManager.notify(0, notification); 
+2
source

Use notification.sound instead of mBuilder.setSound (), solving my problem.

Here is the code -

 Notification notification = mBuilder.build(); notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mNotificationManager.notify(0, notification); 
+2
source

Viewed my sound settings. The notification level was up to zero.

This is difficult because other sounds will work except notifications.

0
source

It may also be due to minor incompatibilities with the old version of the API. For example, when compiling on SDK 25, but targeting an older version (for example, SDK 15), I added the following line to fix the problem:

 notification.audioStreamType = AudioManager.STREAM_ALARM; 
0
source

Source: https://habr.com/ru/post/948663/


All Articles