Home » Android » android – Uri to default sound notification?

android – Uri to default sound notification?

Posted by: admin March 11, 2020 Leave a comment

Questions:

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?

How to&Answers:

try using RingtoneManager to get Default Notification Uri as:

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

builder.setSound(uri);

Answer:

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

Answer:

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

Answer:

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

Answer:

You can use this as well:

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

Answer:

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

Answer:

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.