Uri to default sound notification?

AndroidNotificationsUri

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

try using RingtoneManager to get Default Notification Uri as:

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

builder.setSound(uri);

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

You can use this as well:

Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).
            getString("pref_tone", "content://settings/system/notification_sound"));
mBuilder.setSound(uri);

Solution 7 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefMaView Question on Stackoverflow
Solution 1 - Androidρяσѕρєя KView Answer on Stackoverflow
Solution 2 - AndroidFortegaView Answer on Stackoverflow
Solution 3 - AndroidJorgesysView Answer on Stackoverflow
Solution 4 - AndroidCristiana ChavezView Answer on Stackoverflow
Solution 5 - AndroidsssvrockView Answer on Stackoverflow
Solution 6 - AndroidLeebeedevView Answer on Stackoverflow
Solution 7 - AndroidJevgenij KononovView Answer on Stackoverflow