Firebase Cloud Messaging does not send sound in notification

5

I have two applications, when such an action occurs in one of the two applications, it sends a notification via FCM, to the other application, when the notification arrives, it only makes the notification noise if the application is open, when it is closed notification arrives silently

Here is the code for the app receiving notifications:

public class MyFirebaseMessaging extends FirebaseMessagingService {

@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {

    if (remoteMessage.getNotification().getTitle().equals("Entrega")) {
        showNotificacaoTeste(remoteMessage.getNotification().getBody());
    }
}

private void showNotificacaoTeste(String body) {
    PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 1 ,new Intent(),PendingIntent.FLAG_ONE_SHOT );
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
    builder.setAutoCancel(true)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Entrega")
            .setContentText(body)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
            .setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1,builder.build());
}

}

Code that sends the notification to the other application

 private void cancelarEntrega(String clienteId) {
    Token token = new Token(clienteId);

    Notification notification = new Notification("Entrega", "O entregador não aceitou a entrega");
    Sender sender = new Sender(token.getToken(), notification);

    mFCMService.sendMessage(sender)
            .enqueue(new Callback<FCMResponse>() {
                @Override
                public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                    if (response.body().success == 1) {

                        finish();
                    }
                }

                @Override
                public void onFailure(Call<FCMResponse> call, Throwable t) {

                }
            });
}

Notification Model

public class Notification {

public String title;
public String body;

  public Notification(String title, String body) {
    this.title = title;
    this.body = body;
}
}

Sender Model:

public class Sender {
public String to;
public Notification notification;

public Sender(String to, Notification notification) {
    this.to = to;
    this.notification = notification;
}

}
    
asked by anonymous 28.02.2018 / 21:19

2 answers

3

I'm not sure, but this may be happening because of the type of the message, you are sending a notification of type " Notification ", this causes it be delivered straight to the Android push "tray" and does not pass through your function onMessageReceived .

| Estado do app  | Notificação        | Dados             | Ambos                           |
| -------------- | ------------------ | ----------------- | ------------------------------- |
| Primeiro plano | onMessageReceived  | onMessageReceived | onMessageReceived               |
| Segundo plano  | Bandeja do sistema | onMessageReceived | Notificação: bandeja do sistema |

Send with date type in your Sender class:

public Sender(String to, Notification notification) {
    this.to = to;
    this.data = notification; // <<-- Coloque como "data"
}

And get the data with getData() (I'm not an Android programmer, so I will not be able to help you much in this part ... rsrs)

// Coloque um break-point neste if e verifique se está entrando aqui com o APP fechado
if (remoteMessage.getData().get('title').equals("Entrega")) {
    showNotificacaoTeste(remoteMessage.getData().get('body'));
}

Documentation: onMessageReceived

    
03.03.2018 / 20:34
0

When creating the notification you should call the alarm

private void showNotificacaoTeste(String body) {
    //Uri do som do alarm
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//Pega o alarm padrão do sistema, pode ser personalizado aqui

    PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 1 ,new Intent(),PendingIntent.FLAG_ONE_SHOT );
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
    builder.setAutoCancel(true)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Entrega")
            .setContentText(body)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
            .setContentIntent(contentIntent)
            .setSound(alarmSound);//Som é adicionado a notificação aqui

    NotificationManager manager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1,builder.build());
}

What has changed is only builder.setSound(alarmSound); where sound is added to the notification here

Source link

    
05.03.2018 / 19:44