Flutter 2.0 with Firebase Cloud Messaging: onMessage not called on Android

2,429

There is an issue with firebase_messaging 9.0.0 plugin.

If you navigate to the definition of the factory RemoteMessage.fromMap() Using Cmd+Click or Ctrl+Click while hovering over the RemoteMessage class with the mouse), in the return statement, change

contentAvailable: map['contentAvailable'], 
mutableContent: map['mutableContent'],

to

contentAvailable: map['contentAvailable'] ?? false,
to mutableContent: map['mutableContent'] ?? false,

You may have to confirm that you want to modify the package. This should get you through until the package is updated.

Share:
2,429
Uwinator
Author by

Uwinator

Updated on November 26, 2022

Comments

  • Uwinator
    Uwinator over 1 year

    I got a problem with Firebase Cloud Messaging onMessage in Flutter 2.0.

    The function

    FirebaseMessaging.onMessage.listen((RemoteMessage message) { ... }

    is not called when receiving a message in the foreground. However, the logs say

    broadcast received for message

    At the first received message, I get additional warnings, like:

    Accessing hidden method Landroid/os/WorkSource

    But the warnings disappear on subsequent messages.

    The funny thing is, that

    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    works.

    If I send the app to the background, I get a notification and the defined method is called.

    Code

    @override
    void initState() {
        initializeFlutterFire()
            .then((value) => subscribeToMessages);
    
        super.initState();
    }
    
    Future<void> initializeFlutterFire() async {
        try {
            Firebase.initializeApp();
            setState(() {
                _initialized = true;
                print("Firebase has been initialized");
            });
        } catch (e) {
           setState(() {
               _error = true;
           });
        }
    }
    
    void subscribeToMessages() {
        // The following handler is called, when App is in the background.
        FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    
        // The following function is not called, when a message was received by the device.
        FirebaseMessaging.onMessage.listen((RemoteMessage message) {
            print('Got a message whilst in the foreground!');
            print('Message data: ${message.data}');
    
            if (message.notification != null) {
                print('Message also contained a notification: ${message.notification}');
            }
        });
    }
    
    Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
        print('message from background handler');
        print("Handling a background message: ${message.messageId}");
    }
    

    I got the feeling, that there is no subscription from onMessage().listen(...) and that's why nothing is executed.

    Do you have any suggestions, what I am doing wrong?

    Thanks in advance, Uwe

    Environment

    Firebase Console Cloud Messaging

    on

    Huawei P20 and Samsung Galaxy Tab A 10, both on Android 10.

    pubspec.yaml

    firebase_core: "^1.0.1"
    firebase_messaging: "^9.0.0"
    

    android/build.gradle

    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.5'
    

    android/app/build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    ...
    
    android {
        compileSdkVersion 30
        ...
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 30
            ...
    

    AndroidManifest.xml

    <activity ...>
        ...
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </activity>
    

    flutter doctor

    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel beta, 2.0.2, on macOS 11.2.1 20D75 darwin-x64, locale de)
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    [!] Xcode - develop for iOS and macOS
        ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
        Download at: https://developer.apple.com/xcode/download/
        Or install Xcode via the App Store.
        Once installed, run:
            sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
            sudo xcodebuild -runFirstLaunch
    [✓] Chrome - develop for the web
    [✓] Android Studio
    [✓] Android Studio (version 4.1)
    [✓] IntelliJ IDEA Community Edition (version 2020.1.2)
    [✓] VS Code (version 1.53.0)
    [✓] Connected device (3 available)
    
    ! Doctor found issues in 1 category.