'Firebase FCM data payload received but notification not show up

I test an app using FCM, from the logcat, i notice that the data payload was received after send the fcm from server, but the notification did not show up if turn on the phone screen after about 10++ Minutes received the fcm payload. If turn on the phone screen immediately or within 1-2 minutes after received the fcm, then the notification will show up.

It seem due to the battery management of the Phone (vivo smartphone). If set the battery management for that app to not restricted, then it does show up.

The purpose of the FCM is to send notification to topics subscribers, it is not time critical, so acceptable if delayed, but at least it should show up after some time.

I try to use WorkManager to try show the notification in worker class. The worker did run and can even connect to my web server and query data and output the data in logcat, but fail to show notification if not turn on the phone screen within few minutes after received the FCM.

As mention in other post, some popular app like whatsapp, facebook are whitelisted by the manufacture, but i notice that some other installed app in the phone also able to show notification without change the battery management.

Any suggestion to show the notification, acceptable if notification delayed. Any other workaround?

Thanks in advance

Any mistake in the code below?

public class MyFirebaseInstanceService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseService";

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

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            String message = remoteMessage.getData().get("message").toString();            

            System.out.println("start show notification");

            NotificationManager mNotificationManager =
                    (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

            //****************************
            String channelId = "notfId";

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                String channelName = "Notification FCM";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);
                mNotificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setContentTitle("FCM Notf")
                    .setContentText(message)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE);

            //****************************

            mNotificationManager.notify(100, mBuilder.build());


            /* try to use worker to show notification, also fail to show notification
 if not turn on the screen after few minutes.  If turn on the phone screen immediately 
or within 1-2 minutes after received the fcm, then will show up notification */

            /*
            Constraints constraints = new Constraints.Builder()
                    .setRequiredNetworkType(NetworkType.CONNECTED)
                    .build();

            OneTimeWorkRequest testWorkRequest =
                    new OneTimeWorkRequest.Builder(TestWorker.class)
                            .setConstraints(constraints)
                            .addTag("testWorkerTag")
                            .build();

            WorkManager workManager =  WorkManager.getInstance(getApplicationContext());
            workManager.enqueueUniqueWork("enqueueTestWorker", ExistingWorkPolicy.KEEP, testWorkRequest);
            */



        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
                
    }

    @Override
    public void onNewToken(@NonNull String token) {
        super.onNewToken(token);

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


Sources

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

Source: Stack Overflow

Solution Source