Android Push notification Shows Nothing

Viewed 66

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.

1 Answers

It's very strange, the onMessageReceived method should be called at least in foreground. Please, check the notifications'style and try the example code I have in this repository.

public class PushNotificationHandbookService extends FirebaseMessagingService {

    public static final String TAG = "PushHandbookService";

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        // You will receive the push notifications here!
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Title: " + remoteMessage.getNotification().getTitle() +
                            "Body: " + remoteMessage.getNotification().getBody());
        }
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Title: " + remoteMessage.getData().get("title") +
                            "Body: " + remoteMessage.getData().get("body"));
            sendNotification(remoteMessage);
        }
    }

    ...

    private void sendNotification(RemoteMessage remoteMessage) {
        String title = remoteMessage.getData().get("title");
        String messageBody = remoteMessage.getData().get("body");
        String score = remoteMessage.getData().get("score");
        String country = remoteMessage.getData().get("country");

        Intent intent = new Intent(this, PushReceiverActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("score", score);
        intent.putExtra("country", country);

        @SuppressLint("UnspecifiedImmutableFlag")
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_name)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }
    ...
}

Using a message like this:

var message = {
    notification: {
      title: title,
      body: text
    },
    data: {
      title: title,
      body: text,
      score: '4.5',
      country: 'Canada'
    },
    android: {
      notification: {
        priority: 'high',
        sound: 'default',
        clickAction: '.PushReceiverActivity'
      },
    },
    tokens: registrationTokens
  };
Related