Skip to content
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

Closed
doiraduc opened this issue Aug 30, 2022 · 18 comments · Fixed by #9494
Assignees
Labels
platform: android Issues / PRs which are specifically for Android. plugin: messaging resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working

Comments

@doiraduc
Copy link

doiraduc commented Aug 30, 2022

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 Flutter 3.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:

  1. Use Flutter 3.0.5 from the stable channel.
  2. Run any app that uses firebase_messaging's onBackgroundMessage for the Android platform, but a release version of that app (not a debug or profile one), preferably using flutter run --release.
  3. Go to the home screen.
  4. Try to trigger a background notification.
  • Observe how onBackgroundMessage is called as expected, the logs showing that the message has been handled.
I/flutter (15791): Handling a background message [message id here]
  1. Quit flutter run using q.
  2. Upgrade to Flutter 3.3.0.
  3. Make sure the project does not have any errors, then proceed running the app the same way described in step 2.
  4. Go to the home screen.
  5. Try to trigger a background notification.
  • Observe how 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
[✓] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.22622.586], locale ro-RO)
    • Flutter version 3.0.5 at C:\Users\iamco\fvm\default
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f1875d570e (7 weeks ago), 2022-07-13 11:24:16 -0700
    • Engine revision e85ea0e79c
    • Dart version 2.17.6
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at C:\Users\iamco\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.6)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.2.32630.192
    • Windows 10 SDK version 10.0.19041.0

[✓] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[✓] VS Code (version 1.70.2)
    • VS Code at C:\Users\iamco\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.44.0

[✓] Connected device (4 available)
    • SM A528B (mobile) • R5CR81X9A0N • android-arm64  • Android 12 (API 31)
    • Windows (desktop) • windows     • windows-x64    • Microsoft Windows [Version 10.0.22622.586]
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 104.0.5112.82
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 104.0.1293.70

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
3.3.0
[✓] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.22622.586], locale ro-RO)
    • Flutter version 3.3.0 on channel stable at C:\Users\iamco\fvm\default
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (32 hours ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at C:\Users\iamco\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.6)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.2.32630.192
    • Windows 10 SDK version 10.0.19041.0

[✓] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[✓] VS Code (version 1.70.2)
    • VS Code at C:\Users\iamco\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.44.0

[✓] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22622.586]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 104.0.5112.82
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 104.0.1293.70

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

flutter pub deps --style=compact

3.0.5
Dart SDK 2.17.6
Flutter SDK 3.0.5
allo 0.0.1+400

dependencies:
- cloud_firestore 3.4.6 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cupertino_icons 1.0.5
- email_validator 2.1.17
- firebase_analytics 9.3.3 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 3.7.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]      
- firebase_core 1.21.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 13.0.0 [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 vector_math sky_engine]
- flutter_hooks 0.18.5+1 [flutter]
- flutter_local_notifications 9.9.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- google_sign_in 5.4.1 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web]
- hooks_riverpod 1.0.4 [collection flutter flutter_hooks flutter_riverpod riverpod state_notifier]
- material_plus 0.0.1 [flutter snap_scroll_physics]
- url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]

dev dependencies:
- flutter_lints 2.0.1 [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]

transitive dependencies:
- args 2.3.1
- 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
- cloud_firestore_platform_interface 5.7.3 [collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 2.8.6 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]  
- collection 1.16.0
- dbus 0.7.8 [args ffi meta xml]
- fake_async 1.3.0 [clock collection]
- ffi 2.0.1
- file 6.1.4 [meta path]
- firebase_analytics_platform_interface 3.3.3 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.2+3 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]     
- firebase_auth_platform_interface 6.6.0 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 4.3.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- firebase_core_platform_interface 4.5.1 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 1.7.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 4.1.3 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.1.3 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]  
- flutter_local_notifications_linux 0.5.1 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories]
- flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface]
- flutter_riverpod 1.0.4 [collection flutter meta riverpod state_notifier]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- google_sign_in_android 6.0.1 [flutter google_sign_in_platform_interface]
- google_sign_in_ios 5.4.0 [flutter google_sign_in_platform_interface]
- google_sign_in_platform_interface 2.3.0 [flutter plugin_platform_interface quiver]
- google_sign_in_web 0.10.2 [flutter flutter_web_plugins google_sign_in_platform_interface js]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- intl 0.17.0 [clock path]
- js 0.6.4
- lints 2.0.0
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.4
- meta 1.7.0
- path 1.8.1
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- quiver 3.1.0 [matcher]
- riverpod 1.0.3 [collection meta state_notifier]
- sky_engine 0.0.99
- snap_scroll_physics 0.0.1+3 [flutter]
- source_span 1.8.2 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- state_notifier 0.7.2+1 [meta]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- test_api 0.4.9 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timezone 0.8.0 [path]
- typed_data 1.3.1 [collection]
- url_launcher_android 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.1.0 [flutter plugin_platform_interface]
- url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.2
- xdg_directories 0.2.0+2 [meta path process]
- xml 6.1.0 [collection meta petitparser]
3.3.0
Dart SDK 2.18.0
Flutter SDK 3.3.0
allo 0.0.1+400

dependencies:
- cloud_firestore 3.4.6 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]    
- cupertino_icons 1.0.5
- email_validator 2.1.17
- firebase_analytics 9.3.3 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 3.7.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 1.21.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 13.0.0 [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 vector_math sky_engine]
- flutter_hooks 0.18.5+1 [flutter]
- flutter_local_notifications 9.9.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- google_sign_in 5.4.1 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web]
- hooks_riverpod 1.0.4 [collection flutter flutter_hooks flutter_riverpod riverpod state_notifier]
- material_plus 0.0.1 [flutter snap_scroll_physics]
- url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]

dev dependencies:
- flutter_lints 2.0.1 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]

transitive dependencies:
- args 2.3.1
- async 2.9.0 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.2.1
- clock 1.1.1
- cloud_firestore_platform_interface 5.7.3 [collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 2.8.6 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.16.0
- dbus 0.7.8 [args ffi meta xml]
- fake_async 1.3.1 [clock collection]
- ffi 2.0.1
- file 6.1.4 [meta path]
- firebase_analytics_platform_interface 3.3.3 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.2+3 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_auth_platform_interface 6.6.0 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 4.3.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- firebase_core_platform_interface 4.5.1 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 1.7.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 4.1.3 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.1.3 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- flutter_local_notifications_linux 0.5.1 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories]
- flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface]
- flutter_riverpod 1.0.4 [collection flutter meta riverpod state_notifier]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- google_sign_in_android 6.0.1 [flutter google_sign_in_platform_interface]
- google_sign_in_ios 5.4.0 [flutter google_sign_in_platform_interface]
- google_sign_in_platform_interface 2.3.0 [flutter plugin_platform_interface quiver]
- google_sign_in_web 0.10.2 [flutter flutter_web_plugins google_sign_in_platform_interface js]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- intl 0.17.0 [clock path]
- js 0.6.4
- lints 2.0.0
- matcher 0.12.12 [stack_trace]
- material_color_utilities 0.1.5
- meta 1.8.0
- path 1.8.2
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- quiver 3.1.0 [matcher]
- riverpod 1.0.3 [collection meta state_notifier]
- sky_engine 0.0.99
- snap_scroll_physics 0.0.1+3 [flutter]
- source_span 1.9.0 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- state_notifier 0.7.2+1 [meta]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.1 [source_span]
- term_glyph 1.2.1
- test_api 0.4.12 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timezone 0.8.0 [path]
- typed_data 1.3.1 [collection]
- url_launcher_android 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.1.0 [flutter plugin_platform_interface]
- url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.2
- xdg_directories 0.2.0+2 [meta path process]
- xml 6.1.0 [collection meta petitparser]

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.

@doiraduc doiraduc added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Aug 30, 2022
@amuelle
Copy link

amuelle commented Aug 30, 2022

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
Framework • revision ffccd96b62 (18 hours ago) • 2022-08-29 17:28:57 -0700
Engine • revision 5e9e0e0aa8
Tools • Dart 2.18.0 • DevTools 2.15.0

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.19043.1889], locale de-AT)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.8.4)
[√] Android Studio (version 2021.2)
[√] IntelliJ IDEA Community Edition (version 2018.3)
[√] IntelliJ IDEA Ultimate Edition (version 2019.1)
[√] IntelliJ IDEA Community Edition (version 2020.3)
[√] VS Code (version 1.71.0-insider)
[√] Connected device (3 available)
[√] HTTP Host Availability

• No issues found!

@doiraduc
Copy link
Author

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.
I will update the issue to reflect this.

@doiraduc doiraduc changed the title 🐛 [firebase_messaging] onBackgroundMessage is never called on Flutter beta channel in Android release 🐛 [firebase_messaging] onBackgroundMessage is never called when using Flutter 3.3.0 in release mode on Android Aug 31, 2022
@darshankawar darshankawar added the triage Issue is currently being triaged. label Aug 31, 2022
@darshankawar
Copy link

Thanks for the report @iamcosmin
As you are using plugin's latest verison, 13.0.0, according to changelog, you need to set targetSdkVersion to 33: https://pub.dev/packages/firebase_messaging/changelog#1300

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 darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Aug 31, 2022
@doiraduc
Copy link
Author

@darshankawar Right from the start I set the compileSdkVersion, as well as the targetSdkVersion to 33, but unfortunately this issue persists in more of my projects that are running Flutter 3.3.0.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Aug 31, 2022
@darshankawar
Copy link

When an app using firebase_messaging is built using Flutter 3.3.0, onBackgroundMessage is not being called at all on Android. When using Flutter 3.0.5, the app receives notifications in the background as expected.

I looked at the 3.3.0 release notes / change log, but I don't see any changes that would affect onBackgroundMessage not to work in latest release. So I am not sure why it is not working in 3.3.0 but works in 3.0.5.

Can you provide a complete but minimal reproducible code sample without any third party plugins along with project and app level build.gradle ?

Also, can you try the plugin's official example and see if it works at your end ?

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Aug 31, 2022
@doiraduc
Copy link
Author

I think it may have something to do with Dart 2.18's revamped async/async*/sync* implementation. I'm not quite sure, but I observed this behavior from very early beta builds using Dart 2.18.

As for the sample, here are the files requested:

android/build.gradle
buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
android/app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "com.test.firebase.messaging"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
main.dart
import '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 firebase_messaging example, without the other code that does not make the object of this issue.
The example app was built using flutter create, added firebase_core, firebase_messaging and firebase_local_notifications as per the example in the repo, then ran flutterfire configure.

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 flutter_local_notifications.

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 firebase_local_notifications, and it should do it in release mode too (this works correctly in 3.0.5), but instead, in 3.3.0 all it shows is the native notification, no trace of the flutter_local_notifications generated one.

Considering that the example provided is purely copied from the firebase_messaging example, I think it's more than enough to indicate that this is a real issue, and not dependent by project configuration.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Aug 31, 2022
@darshankawar
Copy link

Thanks for the update. I am keeping this issue open for further insights from the team.

/cc @russellwheatley

@darshankawar darshankawar added plugin: messaging platform: android Issues / PRs which are specifically for Android. and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Sep 1, 2022
@aoancea
Copy link

aoancea commented Sep 4, 2022

I have the same issues as @iamcosmin describes.

I have also downgraded to flutter version 3.0.5 and it seems the notifications work as expected even when build in --release mode. I can also state that I have multile flavours configured, production and development and they both seem to work as expected.

I've also did a release on the development environment which will be build in --release mode and uploaded to Google Play Store and everything works as expected!

cc @iamcosmin

@maeddin
Copy link

maeddin commented Sep 4, 2022

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.

@lannes
Copy link

lannes commented Sep 5, 2022

I just tested Flutter 3.1.0 still ok for onBackgroundMessage

@doiraduc
Copy link
Author

doiraduc commented Sep 5, 2022

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).

@itssidhere
Copy link

itssidhere commented Sep 6, 2022

Same problem. There's a possibility that something is wrong here in this file
firebase_messaging-11.4.4\android\src\main\java\io\flutter\plugins\firebase\messaging\FlutterFirebaseMessagingBackgroundExecutor.java

@xalikoutis
Copy link

xalikoutis commented Sep 6, 2022

Same here after upgrading to 3.3.0 onBackgroundMessage is never called
I can confirm that only happens in release mode. In debug using Logcat everything works great

@russellwheatley russellwheatley self-assigned this Sep 7, 2022
@hatemragab
Copy link

Same problem here also 🥲😓

@russellwheatley
Copy link
Member

Hey folks, if you annotate your background message handler with @pragma('vm:entry-point') like so:

@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.

@HE-LU
Copy link

HE-LU commented Sep 8, 2022

As @russellwheatley said. This actually fixed the issue on our side! Thanks!

@itssidhere
Copy link

Fixed

@darshankawar darshankawar added the resolution: fixed A fix has been merged or is pending merge from a PR. label Sep 9, 2022
@devxpy
Copy link

devxpy commented Sep 10, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform: android Issues / PRs which are specifically for Android. plugin: messaging resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working
Projects
None yet