-
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
🐛 [Cloud Messaging] onBackGroundMessage callback #3905
Comments
Could you paste the part of your code where you register your callback and the code of the callback itself? |
Possibly related? #3819 (comment) |
Hi, here you go. From my main, I run a Material app with InitialPage as it's a child. The following code is from the init of that widget's state class. _firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) =>
_handleNotificationForeground(message),
onLaunch: (Map<String, dynamic> message) =>
_handleNotificationBackground(message),
onResume: (Map<String, dynamic> message) =>
_handleNotificationBackground(message),
onBackgroundMessage: backGroundHandler,
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true,
badge: true,
alert: true,
provisional: true,
),
); Here is my background handler, defined outside the class. Future<dynamic> backGroundHandler(Map<String, dynamic> message) async {
print("onBackgroundMessage: $message");
// await FlutterRingtonePlayer.play(
// android: AndroidSounds.ringtone,
// ios: IosSounds.glass,
// looping: true, // Android only - API >= 28
// volume: 1, // Android only - API >= 28
// asAlarm: true, // Android only - all APIs
// );
Future<void>.value();
} |
Ahh. I think, I have an idea. Apparently, you are testing this on iOS. If you read the docs of the firebase messaging plugin, you will notice that the background message callback is described in the Android section. The reason is that this method is only used on android, as the background process is different there. Therefore, it will never be called on iOS. On iOS the onMessage callback will be used, no matter if the app is in background or not. |
I think there is some confusion. Indeed I am developing an app for both iOS and Android. I have not tested it on iOS yet. I'm facing this problem on Android 11. |
All right then. I'm pretty sure that the print output should appear in the console. Can you try the following?
If the firebase plugin configuration is correctly set up, The app should wait at this breakpoint. |
Messaging rework was announced yesterday https://twitter.com/mikediarmid/status/1319298281290203139. Could this be a potential answer? |
I rather think that the problem is related to this post: At the moment, the firebase messaging plugin doesn't fully support the "new" Flutter embedding classes (introduced in version 1.12) and still relies on the "PluginRegistry.PluginRegistrantCallback". Currently I've also implemeted the solution mentioned in this stackoverflow post. Hopefully, this won't be necessary after the messaging rework. |
Where do I find this? |
Maybe this works. We would have to wait till it is actually launched right? |
So does it mean that we cannot use the onBackGroundMessage handler with the latest flutter version? |
I'm also facing exactly the same issue and behaviour. Not sure how to resolve this :( |
You can use the background message handling, but you need to implement a custom "FirebaseCloudMessagingPluginRegistrant" as a workaround as described in the stackoverflow answer that I've linked above. If you have problems while applying this solution, you can ask them here. |
I am using Kotlin for this part. I found the code on some StackOverflow page. Could that be the issue? Also, how would I modify the fixed Java code to Kotlin? 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.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry?) {
FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
}
} This is my MainActivity.kt import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {} And this is my relevant part of Android manifest |
This should work. The pasted code is already in Kotlin, so what do you mean by the "fixed java code"? However, there is a built-in menu entry in intellij to transform java code to Kotlin: Main Menu -> Code -> Convert Java File to Kotlin File |
By "fixed java code", I was referring to the code from the StackOverflow page. I will try to use the Android Studio tool and see if that helps.
|
Bug report
Describe the bug
Hi
I cannot figure out what can be used in the onBackGroundMessage callback. The other callbacks work fine. I have tried and followed the documentation properly (I think - I double checked). Could you please add this call back to your example - as in do something significant over there.
Steps to reproduce
Steps to reproduce the behavior:
I have tried to print stuff to the console when the app is minimized but it did not work.
I have tried to play a ringtone using the [flutter_ringtone_player] package (which is static) but that does not work either.
Expected behavior
A clear and concise description of what you expected to happen.
Additional context
Add any other context about the problem here.
I want to understand what is supposed to happen and what are the possibilities. Please add that to the example app.
Flutter doctor
Run
flutter doctor
and paste the output below:Click To Expand
Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
The text was updated successfully, but these errors were encountered: