Skip to content

Run Android Native code in background / terminated app #76696

@LuisCampos97

Description

@LuisCampos97

Hi!
I have an application that receives notifications through firebase_messaging, and I needed to build my customizable notification to display.
For this I created native Android code. When I receive the push notification in foreground, I can show the notification without problems, the problem is when I receive the notification in the background it gives an error:
Unhandled Exception: MissingPluginException(No implementation found for method showNotification on channel channel.luis/notifications)

Android Native Code:

public class MainActivity extends FlutterFragmentActivity {

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "channel.luis/notifications").setMethodCallHandler(
                (call, result) -> {
                    if(call.method.equals("showNotification")) {
                        showNotification(call);
                    } else {
                        result.notImplemented();
                    }
                }
        );
    }

Flutter Code

_messaging.configure(
        onBackgroundMessage: Platform.isIOS ? null : onBackgroundMessageHandler,

        //* When app is in foreground (open)
        onMessage: (Map<String, dynamic> message) async {
          NotificationHandler.showNotification(message['data']);
        },

        //* When app is in background
        onResume: (Map<String, dynamic> message) async {},

        //* When app is closed
        onLaunch: (Map<String, dynamic> message) async {});
  }

  static Future onBackgroundMessageHandler(Map<String, dynamic> message) {
    if (message['data'] != null) {
      dynamic data = message['data'];

      NotificationHandler.showNotification(Map<String, dynamic>.from(data));
    }

    return Future<void>.value();
  }
static void showNotification(var message) async {
    const channel = MethodChannel('channel.luis/notifications');

    channel.invokeMethod('showNotification', {
      "title": message['title'],
      "description": message['body'],
      "type": message['ntfType'],
    });
  }

The showNotification of the onMessage method works without problems and shows the customizable notification I made with native Android code. The problem when the push notification arrives in the background in the onBackgroundMessageHandler method it shows the Unhandled Exception: MissingPluginException errorI showed above.

Can someone help me please?

Flutter Doctor -v
[✓] Flutter (Channel stable, 1.22.6, on Linux, locale en_US.UTF-8)
    • Flutter version 1.22.6 at /home/luis/snap/flutter/common/flutter
    • Framework revision 9b2d32b605 (5 weeks ago), 2021-01-22 14:36:39 -0800
    • Engine revision 2f0af37152
    • Dart version 2.10.5

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/luis/android-sdk
    • Platform android-30, build-tools 29.0.2
    • ANDROID_HOME = /home/luis/android-sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~20.10-b08)
    • All Android licenses accepted.

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] Connected device (1 available)
    • KB2003 (mobile) • 950b8db0 • android-arm64 • Android 11 (API 30)
Details

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions