I am working on a Flutter app that receives call notifications via Firebase Cloud Messaging (FCM). The app is expected to ring and navigate to a call screen call_corridor.dart
when a call notification (MessageType: "CONSULTATION") is received, even if the app is in the background or terminated state.
I have tried implementing a foreground service in Android using MethodChannel, and my FirebaseMessagingService in Kotlin receives the notification, but I am facing these issues:
App in background: The notification is received, but it does not trigger the ringing sound.
App in terminated state: The notification comes in but when the notification is clicked, the app seems frozen and it does not open through.
MethodChannel error: MissingPluginException(No implementation found for method startForegroundService on channel ..packagename../foreground).
This is is my main.dart:
Future<SharedPreferences> _initializeApp() async {
try {
WidgetsFlutterBinding.ensureInitialized();
if (Firebase.apps.isEmpty) {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform
);
}
try {
PlatformChannels().setupChannels();
//FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
// Set up error handling
FlutterError.onError =
FirebaseCrashlytics.instance.recordFlutterFatalError;
// Platform specific initialization
if (Platform.isAndroid) {
await FirebaseMessaging.instance.setAutoInitEnabled(true);
} else if (Platform.isIOS) {
await FirebaseMessaging.instance.getAPNSToken();
}
} catch(e){
debugPrint('Firebase services initialization error (may be expected in tests): $e');
}
// Hive initialization
await Hive.initFlutter();
registerHiveAdapters();
await Hive.openBox<NotificationHive>('notifications');
await clearOldNotifications();
// System orientation
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
// Shared preferences
final sharedPref = await SharedPreferences.getInstance();
return sharedPref;
} catch (e, stackTrace) {
debugPrint('Initialization error: $e\n$stackTrace');
rethrow;
}
}
void main() async {
try {
final sharedPref = await _initializeApp();
print("