Uri a la notificación de sonido predeterminada?


123

Yo uso el Notification.Builder para construir una notificación. Ahora quiero usar la notificación de sonido predeterminada con:

builder.setSound(Uri sound)

Pero, ¿dónde está el Uri para la notificación predeterminada?

Respuestas:


267

intente usar RingtoneManager para obtener Uri de notificación predeterminada como:

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

builder.setSound(uri);

50
Gracias. Pero he visto que también puedo usar setDefaults (Notification.DEFAULT_SOUND) ;-)
StefMa

8
puedes pasar este tambiénSettings.System.DEFAULT_NOTIFICATION_URI
Pratik Butani

46

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) funciona bien


55
La configuración proviene de import android.provider.Settings;
Chris Knight

29

Las dos opciones para usar Default Notification Soundson:

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

o usando la clase RingtoneManager :

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

12

todos estos métodos funcionan

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

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

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

Documentación de Google


3

También puedes usar esto:

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

Quiero obtener el applicationsonido del canal, no el sonido predeterminado del sistema probado con NotificationChannel channel = new NotificationChannel(ApplicationClass.getInstance().HighNotificationChannelID, getString(R.string.incoming_sms), NotificationManager.IMPORTANCE_HIGH); channel.getSound();devoluciones, default system soundpor favor ayuda
Sagar

3

Para notificaciones predeterminadas del sistema

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

Para notificaciones personalizadas

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

Fuente del sonido de notificación (cambié el nombre a "giro" y lo coloqué en res-> carpeta sin formato)

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

Generador de notificaciones:

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

0

Si alguien aún lo necesita, esto funciona absolutamente bien con sonido y vibración.

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

Si desea deshabilitar, por ejemplo, la vibración, cambie la vibración a nueva larga [] {0,0,0,0,0}; Algo casi similar que puede hacer con el sonido o usar si otra declaración.

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.