-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Comments
Hi @giorgio79 or you can create Flutter project with java support an follow the steps to implement Closing, as this isn't an issue with |
Thanks Taha. Perhaps the documentaiton could be improved at https://pub.dev/packages/firebase_messaging |
Hi @giorgio79 |
Just found a dupe thread :) #1906 |
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...
|
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...
|
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 |
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 |
Can you share everything you've done? |
You have to update the
and add:
inside the |
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? What version of the actual plugin are yo using in pubspec? What version of the android/app/build.gradle dependency are you using? My mind is blown by this plugin. None of it makes sense, thanks in advance. |
@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. |
I' ve spend around 2 hours to find fix for this. Thanks a lot. You saved me. |
@giorgio79 Did you find a solution yet for this ? |
Buddy, you saved my day. |
@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.
<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>
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() {}
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
}
}
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)
}
} |
thanks a lot @hectorAguero and @hpadrao , |
Totally lost with Kotlin implementation. This issue has been open for 5 months. We need an immediate solution. |
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. |
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.
Application.kt
You then need to update your AndroidManifest.xml to point at these two files. You do this like this:
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.
|
No Application.kt any more. I follow this doc and it works fine.
ps. I am using Flutter Android Embedding V2 Hope this helps. |
I also moved my app from Kotlin to Java. Kotlin first development is a lie and we shouldn't rely on fancy announcements. |
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 |
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. 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. |
i have found the solution for this problem ` package <your package path e.g : com.bla bla bla .bla > import io.flutter.app.FlutterApplication class Application : FlutterApplication(), PluginRegistrantCallback {
} `
|
Documentation needs urgent updating, still no official Kotlin example despite Flutter overtaking the industry! |
firebase_messaging: "^9.0.0" (Null Safety)
|
Not working again. |
I have just updated flutter 2 and write the below code and it works for me
|
transistorsoft/flutter_background_fetch#127 (comment) |
It works 🤩🤩 |
This works for me. |
Hi there, any idea on how to do this with a FlutterFragmentActivity instead? |
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. |
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.
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:
Expected behavior
A clear and concise description of what you expected to happen.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: