126

I use the Notification.Builder to build a notification. Now I want to use the default sound notification with:

builder.setSound(Uri sound)

But where is the Uri to the default notification?

StefMa
  • 3,344
  • 4
  • 27
  • 48

7 Answers7

279

try using RingtoneManager to get Default Notification Uri as:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

builder.setSound(uri);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
46

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) works as well

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Fortega
  • 19,463
  • 14
  • 75
  • 113
29

The two options to use the Default Notification Sound are:

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

or using RingtoneManager Class:

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
12

all of these methods work

  1. mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

  2. mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

Google Documentation

Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
5

For system default notification

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

For custom notification

Uri customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.twirl);

Source of notification sound (I renamed to "twirl" and placed in res->raw folder)

https://notificationsounds.com/message-tones/twirl-470

Notification builder:

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notificaion_icon)
                        .setContentTitle("Title here")
                        .setContentText("Body here")
                        .setSound(defaultSoundUri)
                        .setAutoCancel(true);



NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(id, mBuilder.build());
JJD
  • 50,076
  • 60
  • 203
  • 339
sssvrock
  • 549
  • 7
  • 8
3

You can use this as well:

Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).
            getString("pref_tone", "content://settings/system/notification_sound"));
mBuilder.setSound(uri);
Leebeedev
  • 2,126
  • 22
  • 31
  • I want to get `application` channel sound not default system sound tried with `NotificationChannel channel = new NotificationChannel(ApplicationClass.getInstance().HighNotificationChannelID, getString(R.string.incoming_sms), NotificationManager.IMPORTANCE_HIGH); channel.getSound();` returns `default system sound` please help – Sagar Dec 26 '18 at 12:57
0

If somebody still need, this works absolutely fine with sound and vibration.

        Context context = getApplicationContext();
    long[] vibrate = new long[] { 1000, 1000, 1000, 1000, 1000 };
    Intent notificationIntent = new Intent(context, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Resources res = context.getResources();
    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.notif)
            .setTicker("lastWarning")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setVibrate(vibrate)
            //.setContentTitle(res.getString(R.string.notifytitle)) 
            .setContentTitle("Notification")
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            //.setContentText(res.getString(R.string.notifytext))
            .setContentText("Notification text"); 

    // Notification notification = builder.getNotification(); // until API 16
    Notification notification = builder.build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFY_ID, notification);

If you would like disable for example vibration change vibrate to new long[] { 0,0,0,0,0}; Almost similar thing you can do with sound or use if else statement.

Jevgenij Kononov
  • 1,210
  • 16
  • 11