I am using Firebase Push Notifications and I want to execute some of my code when onBackgroundMessage is triggered. It actually gets triggered because I print in console, but I tried to use several plugins and no luck. I get error every time something like (Unhandled Exception: MissingPluginException(No implementation found for method play on channel flutter_ringtone_player)). I believe this is because there is no context of application in background state of application, but what is this function good for then and what can I actually do in it?
I would like to play sound when onBackgroundMessage is triggered.
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
FlutterRingtonePlayer.play(
android: AndroidSounds.notification,
ios: IosSounds.glass,
looping: true, // Android only - API >= 28
volume: 0.8, // Android only - API >= 28
asAlarm: true, // Android only - all APIs
);
print("background message executed");
return null;
}
Try registering the plugins that you need to use in myBackgroundMessageHandler in your Android's Application class. Here is an example of my Application.kt:
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry?) {
PathProviderPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"))
FlutterLocalNotificationsPlugin.registerWith(registry?.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With