I'm using FCM, everything works well, the notifications are sended using "Data", but when display this happens. I really dont know what to do anymore.
public class CloudMessaging extends FirebaseMessagingService {
String NOTIFICATION_CHANNEL_ID = "Messages_Channel_ID";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
createNotificationChannel();
Map<String, String> data = remoteMessage.getData();
final String title = data.get("title");
final String body = data.get("body");
final String conversation = data.get("conversation");
if (conversation == null) return;
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
builder.setColor(Color.CYAN)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.message_channel_name);
String description = getString(R.string.message_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel messagesChannel = new
NotificationChannel(getString(R.string.Message_Notification_ID), name, importance);
messagesChannel.setDescription(description);
messagesChannel.enableVibration(true);
messagesChannel.setVibrationPattern(new long[]{1000, 200, 500});
messagesChannel.enableLights(true);
messagesChannel.setLightColor(Color.CYAN);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(messagesChannel);
}
}
the backend server, in Firebase Cloud Messaging:
const data = {
token: context.params.token,
data: {
title: document.username,
body: document.message,
conversation: document.conversationid,
},
android: {
notification: {
channel_id: "Messages_Channel_ID",
},
},
};
All the notifications in all sdk's return like this
Edit 1:
Making some tests, and i realize, that the method onreceived is never called, even in back or foreground! Even with the class declared in manifest! But just show de blank notification, because i have the color and icon default declared in the manifest too. If its not declared, probably dont show nothing.