'Firebase Cloud Messaging : not receiving message in android 12

I am trying to send push notification to android 12 device using Firebase messaging service. When i try to send FCM from nodeJs, onMessageReceived is not called. I tried to send FCM from Firebase console, but even the app is in background the app isn't showed any notification or errors. I have tested this app in android 11 and android 10 and both are working fine.

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


<application
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MobileSMSService"
        android:usesCleartextTraffic="true">

<service
        android:name=".helper.MessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/sms_icon_foreground" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/purple_700" />
</service>
</application>

MessagingService.java

public class MessagingService extends FirebaseMessagingService {

    private static final String CHANNEL_ID = "FCM";
    private static final String TAG = "MessagingService";
    whatsappMessageService messageService;

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        JSONObject obj = new JSONObject();
        SharedPreferences sp = getSharedPreferences("Login", MODE_PRIVATE);
        String api = sp.getString("api", null);
        sp.edit().putString("token", s).apply();
        try {
            obj.put("token", s);
            obj.put("api", api);
            new Server().post(new Server().baseUrl + "update-token", obj.toString(), new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {

                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                }
            });
        } catch (Exception e) {
            Log.e("MessagingService", "onNewToken: ", e);
        }
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.e(TAG, "onMessageReceived: recived" );
        Map<String, String> data = remoteMessage.getData();
        if (data.get("web") == null) {
            String message = data.get("message"), to = data.get("to"), plat = data.get("plat");
            if (plat != null) {
                if (plat.equals("w")) {
                    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    boolean isScreenOn = pm.isScreenOn();
                    boolean isScreenOnApi21 = pm.isInteractive();
                    if (!isScreenOn | !isScreenOnApi21) {
                        SharedPreferences sp = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
                        if (sp.getString("wake_phone", null) != null) {
                            sendSMS(sp.getString("wake_phone", null), "Wake up");
                            new MessagesDatabaseHelper(this).insertMessage(sp.getString("wake_phone", null), "Wake up");
                            try {
                                Thread.sleep(3500);
                                isScreenOn = pm.isScreenOn();
                                isScreenOnApi21 = pm.isInteractive();
                                if (!isScreenOn | !isScreenOnApi21) {
                                    Thread.sleep(3500);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            return;
                        }
                    }

                    if (messageService == null) {
                        messageService = new whatsappMessageService(getApplicationContext());
                        messageService.start();
                    }
                    messageService.addMessage(to, message);
                    new WhatsappDatabaseHelper(this).insertMessage(to, message);
                } else {
                    new MessagesDatabaseHelper(this).insertMessage(to, message);
                    sendSMS(to, message);
                }

                Intent intents = new Intent();
                intents.setAction("sms_receiver");
                getBaseContext().sendBroadcast(intents);
            }
        } else {
            Log.e(TAG, "onMessageReceived: " + "web");
            new webManager().onMessage(data);
        }

    }


Solution 1:[1]

For the new people who may come to this issue, I had the exact same issue :
In my case, I was still using Firebase library 17.x with the old deprecated architecture.
As now I updated into the firebase dependency into version 23.0.4, it worked just fine.

Solution 2:[2]

Hello the format of your manifest file is not well kindly check the manifest below and adjust accordingly,also make sure you have a FirebaseMessagingService

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

    <service android:name=".service.MyFcmListenerService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

Also check your build.gradle and add the following above dependancies opening braces

apply plugin: 'com.google.gms.google-services'

Your FirebaseMessagingService should be in the following format:

public class MyFirebaseMessagingService  extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage message) {
    String from = message.getFrom();
    Map data = message.getData();

    Log.e("Message", "Could not parse malformed JSON: \"" + data.toString() + "\"");

    //generateNotification(getApplicationContext(), data);
}

@Override
public void onNewToken(String token) {

    //Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    Toast.makeText(getApplicationContext(),"Token "+token,Toast.LENGTH_LONG).show();
    //App.getInstance().setGcmToken(token);

}

@Override
public void onDeletedMessages() {
    sendNotification("Deleted messages on server");
}

@Override
public void onMessageSent(String msgId) {

    sendNotification("Upstream message sent. Id=" + msgId);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {

    Log.e("Message", "Could not parse malformed JSON: \"" + msg + "\"");
}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmed
Solution 2