🐛 [firebase_messaging] background message handler is not being invoked on a 'release' build on flutter 2.10.0+
Bug report
- The background message handler is not being invoked on a 'release' build using flutter 2.10.0 to 2.10.3 on Android.
- On flutter 2.8.1 and older versions I don't see this issue.
- The issue occurs on multiple versions of firebase_messaging and firebase_core. The most recent versions I have tested are (11.2.11) and firebase_core (1.13.1).
- I have only tested on Android.
- I have tested on a physical device and also on the Android emulator, running Android API 28.
- The issue only happens on a 'release' build of the app, a 'debug' build works as expected.
- In my working example the app is subscribing to a topic, and the FCM message is a data message sent with high priority - so should be delivered to the background message handler, and is in the debug build.
Steps to reproduce
The issue can easily be reproduced using a new flutter android studio project, and doing the bare minimum to make firebase messaging work. In my case I am using Android Studio 2021.1.1 Patch 1
- create a new flutter project
- follow the instructions at https://firebase.flutter.dev/docs/overview/ to setup your project to use firebase messaging
- add firebase_messaging and firebase_core to your pubspec.yaml
- add your app to your firebase project
- release apk built with
flutter build apk --release --obfuscate --split-debug-info build/app/outputs/debug --shrink --split-per-abi - APK installed with
adb install build/app/outputs/apk/release/app-x86_64-release.apk - When running the test app, filter the output of logcat on the string 'XXX' to see when a message is received
- example main.dart below is the standard flutter demo, but with firebase messaging enabled. It just logs when an FCM message is received with the text XXX for easy filtering of logcat
Thank you!
Click To Expand main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("XXX: Handling a background message: ${message.messageId}");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseMessaging.instance.subscribeToTopic('test-topic');
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('XXX: Got a message whilst in the foreground!');
});
runApp(const MyApp());
}
// Everything below this line is just the standard flutter demo app
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Expected behavior
The background message handlder (_firebaseMessagingBackgroundHandler) should be called in 'release' mode when receiving a firebase message.
Flutter doctor
Run flutter doctor and paste the output below:
Click To Expand
martin@ost:/app$ flutter doctor -v
[✓] Flutter (Channel stable, 2.10.3, on Ubuntu 20.04.3 LTS 5.13.0-30-generic, locale en_US)
• Flutter version 2.10.3 at /app/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7e9793dee1 (2 weeks ago), 2022-03-02 11:23:12 -0600
• Engine revision bd539267b4
• Dart version 2.16.1
• DevTools version 2.9.2
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at /home/martin/Android/Sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at: /app/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
• All Android licenses accepted.
[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✓] Android Studio (version 2021.1)
• Android Studio at /app/android-studio
• Flutter plugin version 64.1.2
• Dart plugin version 211.7798
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
[✓] VS Code (version 1.64.2)
• VS Code at /usr/share/code
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)
• Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64 • Android 9 (API 28) (emulator)
[✓] HTTP Host Availability
• All required HTTP hosts are available
! Doctor found issues in 1 category.
Flutter dependencies
Run flutter pub deps -- --style=compact and paste the output below:
Click To Expand
martin@ost:~/StudioProjects/test_app$ flutter pub deps -- --style=compact
Dart SDK 2.16.1
Flutter SDK 2.10.3
test_app 1.0.0+1
dependencies:
- cupertino_icons 1.0.4
- firebase_core 1.13.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 11.2.11 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta typed_data vector_math sky_engine]
dev dependencies:
- flutter_lints 1.0.4 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph typed_data]
transitive dependencies:
- async 2.8.2 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- collection 1.15.0
- fake_async 1.2.0 [clock collection]
- firebase_core_platform_interface 4.2.5 [collection flutter meta plugin_platform_interface]
- firebase_core_web 1.6.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 3.2.1 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 2.2.9 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta typed_data vector_math]
- js 0.6.3
- lints 1.0.1
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.3
- meta 1.7.0
- path 1.8.0
- plugin_platform_interface 2.1.2 [meta]
- sky_engine 0.0.99
- source_span 1.8.1 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- test_api 0.4.8 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0 [collection]
- vector_math 2.1.1
Same issue
Please take a look at #8301, maybe it's related?
Same issue!
- flutter build apk --release --obfuscate --split-debug-info build/app/outputs/debug --shrink --split-per-abi
Can you try without --obfuscate and see if it helps ? I am just trying to think if this flag is causing anything unusual.
Same issue here, I was going crazy figuring the error
Having the same issue :/
- flutter build apk --release --obfuscate --split-debug-info build/app/outputs/debug --shrink --split-per-abi
Can you try without
--obfuscateand see if it helps ? I am just trying to think if this flag is causing anything unusual.
I just tried removing --obfuscate and --shrink too and the issue is still there.
Full command run was:
flutter build apk --release --split-debug-info build/app/outputs/debug --split-per-abi
- In my working example the app is subscribing to a topic, and the FCM message is a data message sent with high priority
Does sending a notification message using FCM work ? or is this specific to data message only ?
- In my working example the app is subscribing to a topic, and the FCM message is a data message sent with high priority
Does sending a notification message using FCM work ? or is this specific to data message only ?
I just tested sending a notification message and it has the same issue.
Everyone who is facing this issue, please upvote original issue description instead of commenting same issue. This will help the team to prioritise based on number of upvotes.
Using the sample code provided in release mode and running with latest plugin version and latest stable, I observed that Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("XXX: Handling a background message: ${message.messageId}");
}
Isn't getting called.
There's a similar issue reported for iOS and some comments indicate that in release mode too, the handler isn't getting called.
Labeling it for further investigation from the team.
/cc @russellwheatley @Salakar
stable flutter doctor -v
[✓] Flutter (Channel stable, 2.10.3, on macOS 12.2.1 21D62 darwin-x64, locale
en-GB)
• Flutter version 2.10.3 at /Users/dhs/documents/fluttersdk/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7e9793dee1 (11 hours ago), 2022-03-02 11:23:12 -0600
• Engine revision bd539267b4
• Dart version 2.16.1
• DevTools version 2.9.2
[!] Xcode - develop for iOS and macOS (Xcode 12.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
! Flutter recommends a minimum Xcode version of 13.
Download the latest version or update via the Mac App Store.
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] VS Code (version 1.62.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.21.0
[✓] Connected device (5 available)
• SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30)
• Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 •
ios • iOS 14.4.1 18D61
• iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 •
ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
• macOS (desktop) • macos •
darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64
• Chrome (web) • chrome •
web-javascript • Google Chrome 98.0.4758.80
[✓] HTTP Host Availability
• All required HTTP hosts are available
! Doctor found issues in 1 category.
@darshankawar is this because of the SHA-1? @martcollins @lore-co @Tom3652 @Hhazeen I just had release push notifications not working and debug working recently and resolved it by adding the SHA-1 (from jks via terminal or play store console) (of course that wouldn't affect ios, so if my comment is noise and irrelevant i apologize, but maybe it helps someone..)
@neiljaywarner, thanks for the suggestion, but I don't think that is it in my case. I have the necessary SHA-1's added to the firebase project. Also, it is working on versions of flutter older than 2.10.0, and in my case has worked since at least flutter 1.17.
Same as @martcollins in my case.
It's on iOS that it's never called but i have read this in the documentation :
On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.
So i don't really understand because the backgroundHandler should actually be used when app is terminated right ? It's at this specific moment that one would like to increase the app badge number for example when receiving a notification ?
Having the same issue, too.
Any solution for this?
same issue.
Please upvote this issue.
same issue
Facing the same issue, are there any updates?