Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification builder sound doesn't work

Tags:

android

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

The notification appears but no sound, any help ?

like image 575
AhlyM Avatar asked Jul 03 '13 22:07

AhlyM


People also ask

Why can't I hear my text messages on my Android phone?

Go to Settings > Sound & Notification > App Notifications. Select the app, and make sure that Notifications are turned on and set to Normal. Make sure that Do Not Disturb is turned off.

How do I change individual app notification sounds android 12?

Tap the Settings button in the in the top-right corner. Tap where it says Sound notifications are active. Tap the toggle switch for each sound to turn it on or off or tap on each sound to set specific sound, vibration and visual alerts.


2 Answers

Set the default flags:

mBuilder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);
like image 167
MichaelS Avatar answered Dec 01 '22 00:12

MichaelS


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

Here is the code -

Notification notification = mBuilder.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager.notify(0, notification);
like image 26
Sunita Avatar answered Dec 01 '22 01:12

Sunita