Firebase Cloud Messaging does not send sound in notification

Asked

Viewed 629 times

4

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

Follow the code of the notifications receiving application:

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

                }
            });
}

Model of the notification

public class Notification {

public String title;
public String body;

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

Model do Sender:

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

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

}
  • Are you sending the message of date type or notification type? Also put the code where you send the notification.

  • I updated the question @Kaduamaral

2 answers

2


I’m not sure, but this could be happening because of the type of message, you’re sending a notification like "Notification", It makes her is delivered directly to the "tray" of push Android and does not pass by its 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 take the dice with getData() (I’m not an Android programmer, so I won’t 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

  • I tested with the date , but it did not work no =/

  • With the date the message is going through onMessageReceived with the app closed?

  • Apparently not

  • Tomorrow I’ll try calmly

  • I updated the response code, when you can test it, let me know.

0

In creating the notification you must call 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’s changed is just builder.setSound(alarmSound); where sound is added to notification here

Source https://stackoverflow.com/a/15855464/4395789

Browser other questions tagged

You are not signed in. Login or sign up in order to post.