Android 11 background FCM with data only and screen off

Viewed 980

On Android 11 only, when the app is in the background and the screen is off, high priority push messages are not received for 10 - 30 minutes.

This is exhibited on a Pixel 4XL running Android 11. General battery optimizations are off. App specific battery optimizations have also been turned off. The device is connected to a WiFi network. The same behaviour is shown on a 4G connection.

From what I understand of the documentation, high priority fcm messages should be delivered immediately and are not restricted to doze cycles.

Even if it was a doze cycle, this behaviour is still evident within 3 minutes of turning the screen off, which is not enough time for deep doze to activate.

The fcm messages work fine while the device is plugged in.

How would I allow my app to receive push messages in a timely manner?

The notification node of the payload is not provided. Only data. The priority is set to high.

1 Answers

I had also faced the same problem in Android 11 especially on Moto G6 & G8. Since FCM was backbone for our application, so I came up with this

  class YourFCMServiceClass : FirebaseMessagingService(){
  override fun onMessageReceived(remoteMessage: RemoteMessage) {
//Your handler Code
}
override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= 26) {
            val CHANNEL_ID = "your_channel_id"
            val channel = NotificationChannel(
                CHANNEL_ID,
                "channel_name",
                NotificationManager.IMPORTANCE_NONE
            )
            (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(
                channel
            )
            val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("")
                .setContentText("").build()
            startForeground(1, notification)
        }
    }


}

Then start the FCM service manually as foreground service

and also implement a BroadcastReceiver for device reboot and again start the service manually as foreground service

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, p1: Intent?) {
        conetxt.startForegroundService(Intent(context, YourFCMServiceClass::class.java))
        
    }
}

This works fine for me.

Related