Flutter Local Notifications and Cloud Firestore Queries in the BACKGROUND

Navin Kodag2 mins

09 May 2021

cat.gif So a project i’ve been working on recently needed a module where i had to receive user notifications in the background and also refresh user’s info as soon as the notification was delivered to a user .

Now, this could’ve been done using the cloud function we were using to send the notification. but we needed to refresh the data only when the user received notification.

so LESGOOOOOOOO! drake.gif the plugins we’ll need:


make sure you’ve set these up before proceeding

run the app once and find the ** **file in android/app/src/main/java/io/flutter/plugins. we’ll need these to identify the import names later.

now head over to the folder containing **MainActivity.kt **and here we’ll create a registrant class.


    package <com.your.application>

    import android.util.Log

    import io.flutter.plugin.common.PluginRegistry

    import io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin

    class CloudFirestorePluginRegistrant {

    companion object {

    fun registerWith(registry: PluginRegistry){

    Log.d("CloudFirestore", "registerWith");

    if(alreadyRegisteredWith(registry)) {

    Log.d("Already Registered","");



    try {


    } catch (e: Exception) {

    Log.d("CloudFirestore", e.toString());}

    Log.d("Plugin Registered","");


    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {

    val key =

    if (registry.hasPlugin(key)) {

    return true



    return false




and now lets register this class in our **Application.kt **in the same folder

    package <com.your.application>


    import io.flutter.plugin.common.PluginRegistry

    import com.your.application.CloudFirestorePluginRegistrant

    class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {

    override fun onCreate() {



    override fun registerWith(registry: PluginRegistry?) {

    if (registry != null) {



and thats about it.

Note: remember to omit “notification” key from our payload to trigger onBackgroundMessage callback.

now we can simply call this code in our firebase messaging onBackgroundMessage callback:

    onBackgroundMessage: _onMyBackGroundMessage;

    _onMybackGroundMessage(Map<String,dynamic> message){



and now we can make cloud firestore queries even if the app is in background. mindblown.gif

Made with Next.js & ❤️
© 2021 Navin Kodag. All rights reserved.