Firebase cloud messaging sound when receiving notification with closed app

1

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());
}}
    
asked by Maxi Aringoli 20.01.2017 в 14:45
source

5 answers

1

I found the answer by my own means. When you send a message from the Firebase console you have to open the advanced options and select sound enabled, which by default is disabled and can not be seen. I leave the images of help.

    
answered by 25.04.2017 / 20:26
source
0

With this it should work ..

notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)

Documentation info.

    
answered by 20.01.2017 в 15:31
0

I control it with these 2 fields, the first one defines the sound, and the second the hums

 .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
 .setVibrate(new long[]{0, 300, 200, 300})

But vibrating needs additional permission:

<uses-permission android:name="android.permission.VIBRATE" />

The issue of priority I control it like this:

import android.app.Notification;
...
.setPriority(Notification.PRIORITY_HIGH)
    
answered by 20.01.2017 в 18:24
0
  

Firebase cloud messaging sound when receiving notification with app   closed.

When receiving a notification (Push notification) is independent if you have open or closed the application because when you install the application and register the device, permissions are given so that the device receives notifications.

What can determine that you can not hear is that you do not define a sound using the setSound() method or that you disable the sound of notifications:

Instead of defining a URI for the sound, it defines the default sound:

//Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//notificationBuilder.setSound(alarmSound);
notificationBuilder.setSound(NotificationCompat.DEFAULT_SOUND);
    
answered by 20.01.2017 в 19:05
0

This is because you are using in the JSON that you send to Firebase the Notification part. This will make Android manage the notification. I recommend you use Body instead of Notification when sending the JSON and on your mobile part

remoteMessage.getData().get("body"));

The same thing happened to me. I recommend Zohab Ali's response to this link

    
answered by 27.05.2017 в 14:16