Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_messaging] Need Kotlin documentation and example #2311

Closed
giorgio79 opened this issue Apr 6, 2020 · 37 comments
Closed

[firebase_messaging] Need Kotlin documentation and example #2311

giorgio79 opened this issue Apr 6, 2020 · 37 comments
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. plugin: messaging type: documentation Improvements or additions to documentation

Comments

@giorgio79
Copy link

Describe the bug
At https://pub.dev/packages/firebase_messaging step 6 of backgrond messaging talks about MainActivity.java but it does not exist for my Flutter project.
image

Looks like a lot of others have this issue https://stackoverflow.com/questions/59225943/flutter-project-mainactivity-java-is-missing

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. '...'
  3. See error or incorrect behavior

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

@giorgio79 giorgio79 added the type: bug Something isn't working label Apr 6, 2020
@TahaTesser
Copy link

Hi @giorgio79
Your project has Kotlin instead of Java . You can implement MainActivity.kt from this answer

or you can create Flutter project with java support an follow the steps to implement firebase_messaging

Closing, as this isn't an issue with firebase_messaging itself,
if you disagree please write in the comments and I will reopen it
Thank you

@giorgio79
Copy link
Author

Thanks Taha. Perhaps the documentaiton could be improved at https://pub.dev/packages/firebase_messaging
I assume the kotlin based projects will prevail over pure Java, as Flutter rocks.

@TahaTesser
Copy link

TahaTesser commented Apr 6, 2020

Hi @giorgio79
Yes, it should be updated

@giorgio79
Copy link
Author

Just found a dupe thread :) #1906

@giorgio79
Copy link
Author

giorgio79 commented Apr 7, 2020

PS, it is also unclear whether we should paste the stuff after what is in MainActivity.kt or overwrite what is in there? For now, I just pasted after what was in there...

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
//https://stackoverflow.com/questions/59984162/firebase-messaging-handle-background-message-in-kotlin/60634673#60634673
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService


class Application : FlutterApplication(), PluginRegistrantCallback {
    fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry)
    }
}

@giorgio79
Copy link
Author

giorgio79 commented Apr 7, 2020

Ok, I think the Google team needs to do a better job. I am unsuccessful so far in updating my main Kotlin file, getting errors like this based on the linked stackoverflow kotlin example... The imports are also totally different from the default kotlin file provided out of the box...

package me.exampleapp

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
//https://stackoverflow.com/questions/59984162/firebase-messaging-handle-background-message-in-kotlin/60634673#60634673
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService


class Application : FlutterApplication(), PluginRegistrantCallback {
    fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry)
    }
}

Launching lib\main.dart on Pixel 2 in debug mode...
Running Gradle task 'assembleDebug'...
C:\AndroidStudioProjects\exampleapp\android\app\src\main\kotlin\me\exampleapp\MainActivity.kt: (15, 9): 'onCreate' hides member of supertype 'FlutterApplication' and needs 'override' modifier
C:\AndroidStudioProjects\exampleapp\android\app\src\main\kotlin\me\exampleapp\MainActivity.kt: (20, 9): 'registerWith' hides member of supertype 'PluginRegistrantCallback' and needs 'override' modifier
C:\AndroidStudioProjects\exampleapp\android\app\src\main\kotlin\me\exampleapp\MainActivity.kt: (21, 48): Type mismatch: inferred type is PluginRegistry? but FlutterEngine was expected

FAILURE: Build failed with an exception.

@giorgio79
Copy link
Author

Lol I am finding so many issues around this and the plugin page has no up to date documentation ... WTF Simple carelessness...

#1684
#1613

@TahaTesser TahaTesser reopened this Apr 7, 2020
@TahaTesser TahaTesser changed the title [firebase_messaging] Flutter - No MainActivity.java exists [firebase_messaging] Need Kotlin documentation and example Apr 7, 2020
@TahaTesser TahaTesser added type: documentation Improvements or additions to documentation and removed type: bug Something isn't working labels Apr 7, 2020
@giorgio79
Copy link
Author

Thanks Taha! Looking forward to a working Kotlin / Flutter example. The example linked from firebase_messaging pub dart page is 3 years old and Java based... https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/example

@Ehesp Ehesp added impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging labels Apr 20, 2020
@hectorAguero
Copy link

hectorAguero commented Apr 20, 2020

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.

Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

@joshua1996
Copy link

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.

Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

Yes, It works! Thank You

@ghost
Copy link

ghost commented May 19, 2020

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.

Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

Can you share everything you've done?
Did you remove your MainActivity file? Did you update your AndroidManifest.xml android:name?

@hpadrao
Copy link

hpadrao commented May 19, 2020

You have to update the AndroidManifest.xml as well.

-        android:name="io.flutter.app.FlutterApplication"
+        android:name=".Application"

and add:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

inside the <activity tag.

@ghost
Copy link

ghost commented May 19, 2020

You have to update the AndroidManifest.xml as well.

-        android:name="io.flutter.app.FlutterApplication"
+        android:name=".Application"

and add:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

inside the <activity tag.

Is there more of your implementation you can share? Like a spoonfeeding of information?

How did you declare your myBackgroundMessageHandler function?

What are you using for MainActivity.kt?

What are you using for Application.kt?

What does your AndroidManifest have in it?
Does the package name at the top match what's in google-services.json? Does it match what's in the MainActivity.kt?

What version of the actual plugin are yo using in pubspec?

What version of the android/app/build.gradle dependency are you using?
Ex: implementation "com.google.firebase:firebase-messaging:XXXX

My mind is blown by this plugin. None of it makes sense, thanks in advance.

@hectorAguero
Copy link

@Bretie you need to follow the pub.dev documentation https://pub.dev/packages/firebase_messaging

The only thing that change from that for flutter kotlin projects is the code below the step that says:

2. Add an Application.java class to your app in the same directory as your MainActivity.java. This is typically found in /android/app/src/main/java//.

If the proccess is too complicated you can search for a youtube video

@ghost
Copy link

ghost commented May 21, 2020

@Bretie you need to follow the pub.dev documentation https://pub.dev/packages/firebase_messaging

The only thing that change from that for flutter kotlin projects is the code below the step that says:

2. Add an Application.java class to your app in the same directory as your MainActivity.java. This is typically found in /android/app/src/main/java//.

If the proccess is too complicated you can search for a youtube video

I can assure you it's not too complicated. When it comes to background messages using this plug it tends to be inconsistent compatibility. It requires unique steps for every other person that aren't documented. Hence, me asking for your full implementation.

@lakshanmamalgaha
Copy link

lakshanmamalgaha commented Jun 9, 2020

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.

Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

I' ve spend around 2 hours to find fix for this. Thanks a lot. You saved me.

@TahaTesser TahaTesser added the platform: android Issues / PRs which are specifically for Android. label Jun 21, 2020
@randika-srimal
Copy link

@giorgio79 Did you find a solution yet for this ?

@Purvik
Copy link

Purvik commented Jul 16, 2020

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.
Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

I' ve spend around 2 hours to find fix for this. Thanks a lot. You saved me.

Buddy, you saved my day.

@randika-srimal
Copy link

@giorgio79 , I struggled with this for many hours. Even the above solutions didn't work for me. Finally found a way solve this. Here is how I managed to fix this.

  1. Change AndroidManifest.xml name properties as below.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
    <application
        android:name=".Application">
           <activity
            android:name=".MainActivity">
           </activity>
    </application>
</manifest>
  1. Empty MainActivity.kt file as below.
package com.example.app

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {}
  1. Add FirebaseCloudMessagingPluginRegistrant.kt file in same directory.
package com.example.app

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry?) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry?): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
        if (registry?.hasPlugin(key)!!) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}
  1. Add Application.kt file in same directory.
package com.example.app

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
    }
}

@fahmisatrio
Copy link

You have to update the AndroidManifest.xml as well.

-        android:name="io.flutter.app.FlutterApplication"
+        android:name=".Application"

and add:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

inside the <activity tag.

thanks a lot @hectorAguero and @hpadrao ,
i misstype changing android:name --> .Application inside activity instead of android:name inside application tag.

@deadsoul44
Copy link

Totally lost with Kotlin implementation. This issue has been open for 5 months. We need an immediate solution.

@EdwinOlivera
Copy link

EdwinOlivera commented Sep 2, 2020

@giorgio79 , I struggled with this for many hours. Even the above solutions didn't work for me. Finally found a way solve this. Here is how I managed to fix this.

  1. Change AndroidManifest.xml name properties as below.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
    <application
        android:name=".Application">
           <activity
            android:name=".MainActivity">
           </activity>
    </application>
</manifest>
  1. Empty MainActivity.kt file as below.
package com.example.app

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {}
  1. Add FirebaseCloudMessagingPluginRegistrant.kt file in same directory.
package com.example.app

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry?) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry?): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
        if (registry?.hasPlugin(key)!!) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}
  1. Add Application.kt file in same directory.
package com.example.app

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
    }
}

Esta es la solucion al problema. A mi me funcion perfectamente. No me habia dado cuenta que no tenia los archivos en un mismo directorio. Espero que esto le sirva a otras personas.

@robgry
Copy link

robgry commented Oct 17, 2020

This took me a while to figure out but here is a working solution:

You'll need two files in the SAME directory as your MainActivity.kt. The two files:

MainActivity.kt

This file will stay the same.

package com.example.app

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

Application.kt

package com.example.app

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

You then need to update your AndroidManifest.xml to point at these two files. You do this like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
         <!-- 
           <application
        android:name="io.flutter.app.FlutterApplication"
         -->
    <application
        android:name=".Application"
        android:label="example"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"

So, you've essentially replaced the application file and kept the main activity the same.

Don't forget

You need to drop this in the AndroidManifest.xml file as well within the activity tags.

<intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

@ming-chu
Copy link

ming-chu commented Nov 5, 2020

No Application.kt any more.

I follow this doc and it works fine.
https://firebase.flutter.dev/docs/messaging/overview

firebase_core: ^0.5.1
firebase_messaging: ^8.0.0-dev.5

ps. I am using Flutter Android Embedding V2

Hope this helps.

@deadsoul44
Copy link

I also moved my app from Kotlin to Java. Kotlin first development is a lie and we shouldn't rely on fancy announcements.

@KaungZawHtet
Copy link

Deprecated doc killing many apps including mine. There r many solutions but I don't know which one should be final. Plz consider to update the doc

@zstima
Copy link

zstima commented Nov 9, 2020

didnt read a lot of these posts, but i managed to get push working on an emulator, running a flutter build.

the main thing u gotta use is PlatformChannel, to call a method written in kt/java files from the flutter widget or flutter file.
u gotta setup the channel on both sides of the code, so flutter calls method using PlatformChannel and kotlin/java recieves the call.

i succesfully got a push message from the console on a flutter app.

the instructions provided online are OK but somewhat confusing with all the classes (somewhat bulky) u gotta use, especially if ure not experienced.

@mohadel92
Copy link

mohadel92 commented Nov 16, 2020

i have found the solution for this problem
first you have to create 2 files in the same directory of MainActivity.kt:
1- Application.kt:

` package <your package path e.g : com.bla bla bla .bla >

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
}

override fun registerWith(registry: PluginRegistry?) {
    io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}

} `

`package <your package path e.g : com.bla bla bla .bla >

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry?) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry?): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
        if (registry?.hasPlugin(key)!!) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}`

3- then modify MainActivity.kt to look like:
`package <your package path e.g : com.bla bla bla .bla >

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
`

4- reference MainActivity.kt and Application.kt in Androidmainfest.xml file:
a- `<application
        android:name="com.blablabla.Application"` 
b-`<activity android:name="com.blablabla.MainActivity"`

@Karlheinzniebuhr
Copy link

Documentation needs urgent updating, still no official Kotlin example despite Flutter overtaking the industry!

@abdullah432
Copy link

firebase_messaging: "^9.0.0" (Null Safety)

com.example.yourpackagename;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;

class Application : FlutterApplication(), PluginRegistrantCallback {

  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this)
  }


  override fun registerWith(registry: PluginRegistry?) {
        io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin"));
    }

}

@abdullah432
Copy link

PluginRegistrantCallback

Not working again.
"PluginRegistrantCallback" is deprecated now. Any solution?

@uc-dve
Copy link

uc-dve commented Mar 17, 2021

PluginRegistrantCallback

Not working again.
"PluginRegistrantCallback" is deprecated now. Any solution?

I have just updated flutter 2 and write the below code and it works for me

package com.flutter.yourPackageName
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
    }
}

@abdullah432
Copy link

PluginRegistrantCallback

Not working again.
"PluginRegistrantCallback" is deprecated now. Any solution?

I have just updated flutter 2 and write the below code and it works for me

package com.flutter.yourPackageName
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
    }
}

transistorsoft/flutter_background_fetch#127 (comment)
Please check out this answer, here this dude is saying that we no longer need custom .Application class. I follow him and the issue now get resolved.

@SAMYAK99
Copy link

SAMYAK99 commented Apr 9, 2021

Update: based in another plugin that I'm using in another app, I'm ussing this code for handle background message in kotlin... I don't know is better than stackoverflow answer, but works.

Application.kt

package your.app.package

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

It works 🤩🤩

@rs-anhnq2
Copy link

@giorgio79 , I struggled with this for many hours. Even the above solutions didn't work for me. Finally found a way solve this. Here is how I managed to fix this.

  1. Change AndroidManifest.xml name properties as below.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
    <application
        android:name=".Application">
           <activity
            android:name=".MainActivity">
           </activity>
    </application>
</manifest>
  1. Empty MainActivity.kt file as below.
package com.example.app

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {}
  1. Add FirebaseCloudMessagingPluginRegistrant.kt file in same directory.
package com.example.app

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry?) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry?): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
        if (registry?.hasPlugin(key)!!) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}
  1. Add Application.kt file in same directory.
package com.example.app

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
    }
}

This works for me.
#2311 (comment)
Document said that: "If you are using Flutter Android Embedding V2 (Flutter Version >= 1.12) then no additional integration steps are required for Android."
But my flutter version 1.22.6, firebase_messaging: ^7.0.3, cloud_firestore: ^0.14.4 still get this issue.
Thank you. You save my day!

@JaviCore
Copy link

JaviCore commented Feb 2, 2022

Hi there, any idea on how to do this with a FlutterFragmentActivity instead?

@Lyokone
Copy link
Contributor

Lyokone commented Sep 9, 2022

Official documentation is not specific to Java now so I'm closing this issue :)

@JaviCore feel free to open a new issue if you have issues.

@Lyokone Lyokone closed this as completed Sep 9, 2022
@firebase firebase locked and limited conversation to collaborators Oct 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. plugin: messaging type: documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests