-
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
🐛 [firebase_messaging] onBackgroundMessage
is never called when using Flutter 3.3.0 in release mode on Android
#9446
Comments
After flutter upgrade to 3.3.0 on stable channel the app (in release mode) doesn't receive any notification in the background Flutter doctor output Flutter 3.3.0 • channel stable • https://github.com/flutter/flutter.git Running flutter doctor... • No issues found! |
Shortly after I created this issue, Flutter released the stable version of 3.3.0, and I was really wondering if it has the same behavior. It seems like it does. |
onBackgroundMessage
is never called on Flutter beta channel in Android releaseonBackgroundMessage
is never called when using Flutter 3.3.0 in release mode on Android
Thanks for the report @iamcosmin Do you have it set already to 33 ? If not, can you try the same and see if you get same behavior or not ? |
@darshankawar Right from the start I set the |
I looked at the 3.3.0 release notes / change log, but I don't see any changes that would affect Can you provide a complete but minimal reproducible code sample without any third party plugins along with project and app level Also, can you try the plugin's official example and see if it works at your end ? |
I think it may have something to do with Dart 2.18's revamped As for the sample, here are the files requested: android/build.gradle
android/app/build.gradle
main.dartimport 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'firebase_options.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await setupFlutterNotifications();
showFlutterNotification(message);
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
print('Handling a background message ${message.messageId}');
}
/// Create a [AndroidNotificationChannel] for heads up notifications
late AndroidNotificationChannel channel;
bool isFlutterLocalNotificationsInitialized = false;
Future<void> setupFlutterNotifications() async {
if (isFlutterLocalNotificationsInitialized) {
return;
}
channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description:
'This channel is used for important notifications.', // description
importance: Importance.high,
);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
/// Create an Android Notification Channel.
///
/// We use this channel in the `AndroidManifest.xml` file to override the
/// default FCM channel to enable heads up notifications.
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
isFlutterLocalNotificationsInitialized = true;
}
void showFlutterNotification(RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
),
);
}
}
/// Initialize the [FlutterLocalNotificationsPlugin] package.
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// Added requestPermission becauase of Android 13.
await FirebaseMessaging.instance.requestPermission();
// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb) {
await setupFlutterNotifications();
}
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Notifications test'),
),
),
),
);
}
If you look closer, the example app that I'm providing is, more or less, direct copy of the official If you follow the steps provided in the first comment, you'll see that the app does not register in the console any event of a received background notification, which it should (this behavior is present in 3.0.5), neither it shows a notification made by I have to note again that this issue only occurs on release builds and only on Android platforms (I have tested this in an emulator with Android 13, as well as a Samsung device running Android 12). In debug and profile mode, the app receives 2 notifications, one from the native Firebase Messaging, and one from the Flutter background handler's Considering that the example provided is purely copied from the |
Thanks for the update. I am keeping this issue open for further insights from the team. /cc @russellwheatley |
I have the same issues as @iamcosmin describes. I have also downgraded to flutter version I've also did a release on the cc @iamcosmin |
I have the same problem but can't downgrade to Flutter 3.0.5 because my app depends on SDK 33 and Flutter 3.0.5 doesn't work with SDK 33 as compileSdkVersion. |
I just tested Flutter 3.1.0 still ok for onBackgroundMessage |
It may work, but from what I know, Flutter 3.1.0 is a early beta release from June I think. It may have performance issues, bugs or partially implemented items. But even if you could use Flutter 3.1.0, you would assume that a stable release, which is also minor would not cause an issue that important (firebase messaging worked when flutter jumped from 1.x to 2.x, same for 2.x to 3.x, but it won't work when jumping from 3.0 to 3.3). |
Same problem. There's a possibility that something is wrong here in this file |
Same here after upgrading to 3.3.0 |
Same problem here also 🥲😓 |
Hey folks, if you annotate your background message handler with @pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// handle message
} It will stop it from being removed during tree shaking when building for release mode. I guess Flutter 3.3 has stricter tree shaking capability and was removing the function. Documentation PR to follow. |
As @russellwheatley said. This actually fixed the issue on our side! Thanks! |
Fixed |
Things like these really need to be mentioned in the breaking changes! This is a very critical bug for anyone relying on firebase_messaging for notifications |
Bug report
When an app using firebase_messaging is built using Flutter
3.3.0
,onBackgroundMessage
is not being called at all on Android. When using Flutter3.0.5
, the app receives notifications in the background as expected.Weirdly enough, this issue only occurs when the app is built for release (using either
flutter run --release
or building any Android-compatible installation packages, such as .apk or .aab).Steps to reproduce
Steps to reproduce the behavior:
onBackgroundMessage
for the Android platform, but a release version of that app (not a debug or profile one), preferably usingflutter run --release
.onBackgroundMessage
is called as expected, the logs showing that the message has been handled.q
.onBackgroundMessage
is not being called at all, the logs not even showing that a background message has been handled (like at step 4).flutter doctor -v
3.0.5
3.3.0
flutter pub deps --style=compact
3.0.5
3.3.0
Thank you!
Edit 1: Updated to reflect that the issue occurs also in Flutter release, as Flutter released 3.3.0 stable a few hours after I created this issue.
The text was updated successfully, but these errors were encountered: