Good morning, I'm having this little problem that I can not find a solution for. I set up my app to receive push notifications with FCM, the issue is that when I have the app closed the notification but does not play sound and that is what I would like to achieve. Any hand with this?
With this, when the app is open, it works correctly.
public class BarilochePcMessaginService extends FirebaseMessagingService {
private static final String LOGTAG = "android-fcm";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
String titulo = remoteMessage.getNotification().getTitle();
String texto = remoteMessage.getNotification().getBody();
Log.d(LOGTAG, "NOTIFICACION RECIBIDA");
Log.d(LOGTAG, "Título: " + titulo);
Log.d(LOGTAG, "Texto: " + texto);
//Opcional: mostramos la notificación en la barra de estado
showNotification(titulo, texto);
}
}
private void showNotification(String title, String text) {
String titulo = (title == null || title.isEmpty()) ? "Notificación importante" : title;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icono)
.setContentTitle(titulo)
.setContentText(text)
.setAutoCancel(true);
Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contIntent = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, 0);
notificationBuilder.setContentIntent(contIntent);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}}