studio priority overview notification example ejemplo anatomy android android-notifications

priority - Android Notification Sound



notificationmanager android ejemplo (11)

Lo que faltaba en mi código anterior:

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound);

He utilizado el creador NotificationCompat más nuevo y no puedo recibir la notificación para hacer un sonido. Vibrará y destellará la luz. La documentación de Android dice que establezca un estilo que he hecho con:

builder.setStyle(new NotificationCompat.InboxStyle());

Pero no hay sonido?

El código completo:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notifications Example") .setContentText("This is a test notification"); Intent notificationIntent = new Intent(this, MenuScreen.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); builder.setAutoCancel(true); builder.setLights(Color.BLUE, 500, 500); long[] pattern = {500,500,500,500,500,500,500,500,500}; builder.setVibrate(pattern); builder.setStyle(new NotificationCompat.InboxStyle()); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, builder.build());


Otra forma para el sonido predeterminado

builder.setDefaults(Notification.DEFAULT_SOUND);


Primero coloque el archivo .mp3 "yourmp3file" en la carpeta raw (es decir, dentro de la carpeta Res)

2º en tu código puesto ...

Notification noti = new Notification.Builder(this) .setSound(Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.yourmp3file))//*see note

Esto es lo que puse en mi onClick (Ver v) ya que solo "context (). GetPackageName ()" no funcionará desde allí ya que no tendrá ningún contexto


Puedes hacer lo siguiente:

MediaPlayer mp; mp =MediaPlayer.create(Activity_Order_Visor_Atender.this, R.raw.ok); mp.start();

Usted crea un paquete entre sus recursos con el nombre de raw y allí guarda sus sonidos, entonces simplemente lo llama.


Puedes hacer una función:

public void playNotificationSound() { try { Uri alarmSound = `Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.getInstance().getApplicationContext().getPackageName() + "/raw/notification");` Ringtone r = RingtoneManager.getRingtone(MyApplication.getInstance().getApplicationContext(), alarmSound); r.play(); } catch (Exception e) { e.printStackTrace(); } }

Llame a esta función cuando reciba una notificación.

Aquí raw es la carpeta en res y la notificación es el archivo de sonido en la carpeta sin formato.


Simplemente coloque su archivo de sonido en la carpeta Res/raw/siren.mp3 , luego use este código:

Para sonido personalizado:

Notification notification = builder.build(); notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);

Para el sonido predeterminado:

notification.defaults |= Notification.DEFAULT_SOUND;

Para Custom Vibrate:

long[] vibrate = { 0, 100, 200, 300 }; notification.vibrate = vibrate;

Para la vibración predeterminada:

notification.defaults |= Notification.DEFAULT_VIBRATE;


Simplemente ponga el siguiente código simple:

notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound_file);

Para el sonido predeterminado:

notification.defaults |= Notification.DEFAULT_SOUND;


Tienes que usar RingtoneManager

private static final int MY_NOTIFICATION_ID = 1; private NotificationManager notificationManager; private Notification myNotification; private final String myBlog = "http://niravranpara.blogspot.com/";

Código para noficationmanager con tono de llamada de alarma también puede establecer el tono de timbre RingtoneManager.TYPE_RINGTONE

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri .parse(myBlog)); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification note = new Notification(R.drawable.ic_launcher, "Alarm", System.currentTimeMillis()); note.setLatestEventInfo(getApplicationContext(), "Alarm", "sound" + " (alarm)", pi); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } note.sound = alarmSound; note.defaults |= Notification.DEFAULT_VIBRATE; note.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(MY_NOTIFICATION_ID, note);


Tienes que usar el constructor. setSound

Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); builder.setAutoCancel(true); builder.setLights(Color.BLUE, 500, 500); long[] pattern = {500,500,500,500,500,500,500,500,500}; builder.setVibrate(pattern); builder.setStyle(new NotificationCompat.InboxStyle()); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if(alarmSound == null){ alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); builder.setSound(alarmSound); manager.notify(1, builder.build());


USO Puede codificar

String en_alert, th_alert, en_title, th_title, id; int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage; class method Intent intent = new Intent(context, ReserveStatusActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); intent = new Intent(String.valueOf(PushActivity.class)); intent.putExtra("message", MESSAGE); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(PushActivity.class); stackBuilder.addNextIntent(intent); // PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); // android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle(); // bigStyle.bigText((CharSequence) context); notification = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(th_title) .setContentText(th_alert) .setAutoCancel(true) // .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า // .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title)) .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert)) .setContentIntent(pendingIntent) .setNumber(++numMessages) .build(); notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationManager.notify(1000, notification);


notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);