flutter – The background message handler function from firebase messaging is not running on iOS


I have a simple app that uses the Firebase Cloud Messaging service (the firebase_messaging package) to send push notifications to mobile devices. After following the official setup documentation on both devices, the library documentation suggests that the statement of FirebaseMessaging.onBackgroundMessage(...) registers a top-level function to be executed in the background as a means of capturing the incoming RemoteMessage. However, this function is only supposed to run while the app is running in the background. Yet, it is running on Android but not on iOS. What could be causing this problem?

I performed all the testing on a physical iOS device and followed the iOS setup documentation. In fact, I can confidently say that foreground messages are being successfully received on the physical iOS device. However, only the function that handles background messages is the one not being executed by the library for some reason even though it has been registered for future execution.

If someone manages to make this function run, please share any steps that unfortunately the library documentation is not mentioning.

../lib/main.dart:

import 'package:flutter/material.dart';
import '/services/firebase_service.dart';
import '/screens/main_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FirebaseService.initialize();

  runApp(const Application());
}

class Application extends StatelessWidget {
  const Application({super.key});

  @override
  Widget build(BuildContext context) {
    final appTheme = ThemeData(
      colorScheme: ColorScheme.fromSeed(
        seedColor: Colors.deepPurple,
      ),
    );

    return MaterialApp(
      theme: appTheme,
      title: 'AppSample',
      home: const MainScreen(),
    );
  }
}

../lib/services/firebase_service.dart:

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

class FirebaseService {
  FirebaseService._();

  static Future<void> backgroundMessageReceived(RemoteMessage message) async {
    // This function is NOT being executed in iOS for some reason ...
  }

  static Future<void> foregroundMessageReceived(RemoteMessage message) async {
    // This function is being executed on both platforms without any problem.
  }

  static Future<void> backgroundMessageTapped(RemoteMessage message) async {
    // This function is being executed on both platforms without any problem.
  }

  static Future<void> initialize() async {
    const initDelay = Duration(milliseconds: 300);
    final fbc = await Firebase.initializeApp();
    final fcm = FirebaseMessaging.instance;

    await Future.delayed(initDelay, () async {
      final fcmConfig = await fcm.requestPermission();
      final fcmToken = await fcm.getToken();
      print('DeviceToken: $fcmToken');
    });

    FirebaseMessaging.onMessage.listen(foregroundMessageReceived);
    FirebaseMessaging.onMessageOpenedApp.listen(backgroundMessageTapped);
    FirebaseMessaging.onBackgroundMessage(backgroundMessageReceived);
  }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img