I have an android application with firebase messaging, the notifications when I arrive I show them with a personalized notification (sound, color, vibration, icon, ...). All good until there, when you have the application open, but now when the app is closed, and send the notification, it is shown with a basic notification (without sound, with the icon of the app, without vibration). it does not show it with my personalized notification.
public class MyFirebaseMessageReceived extends FirebaseMessagingService {
private final String TAG = "MessageReceived";
private SharedPreferences spNotification;
private SharedPreferences.Editor spEditor;
private static Set<String> listaNotificaciones;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData().toString());
String tipoMensaje = remoteMessage.getData().get("tipoMensaje").toString();
String body = remoteMessage.getNotification().getBody();
String titulo = remoteMessage.getNotification().getTitle();
switch (tipoMensaje) {
case "hora":
mostrarNotificacion(construirNotificacionHora(titulo, body, tipoMensaje), 2);
break;
}
}
}
private void mostrarNotificacion(Notification notification, int id) {
//NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
}
private Notification construirNotificacionHora(String titulo, String body, String chanelId) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, chanelId) // ANDROID 8
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher_background)
.setColor(Color.parseColor("#71b32a"))
.setContentTitle(titulo)
.setContentText(body)
.setAutoCancel(true);
//.setContentIntent(resultPendingIntent);
return builder.build();
}
}
I also add that the notifications sent to my cell phone are from my php server, and not from the firebase page.
class messengerServiceFCM {
var $url = "https://fcm.googleapis.com/fcm/send";
public function __construct() {
}
public function sendMessenger($autorization, $token, $tipoMensaje, $title, $body) {
$fields = json_encode(Array(
// "registration_ids" => "",
// "condition" => "",
//"to" => $token, // "/topics/androide", //"/topics/androide"
"registration_ids" => $token,
"notification" => Array(
"title" => $title,
"body" => $body
),
"data" => Array(
"tipoMensaje" => $tipoMensaje
)
));
$headers = Array(
'Content-Type: application/json',
'Authorization:key=' . $autorization
);
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
if ($result == FALSE) {
return (["error" => curl_error($ch)]);
exit;
}
curl_close($ch);
return json_decode($result);
}