Flutterfire: [firebase_messaging] Error when using backgroundMessageHander as per docs

Created on 25 Feb 2020  路  12Comments  路  Source: FirebaseExtended/flutterfire

Hi! I set up a firebase cloud messing for my flutter application.
I using backgoundMessageHandler and I implemented like that documentation but I take errors:

  1. If I do not implement backgroundMessagehandler when I push notification from the firebase console then my app crash.
  2. When I set up native code like that

Screen Shot 2020-02-25 at 10 34 18 AM

In the documentation then GeneratedPluginRegistrant.registerWith(registry); with input is PluginRegistry but in the native code (Kotlin) then input is FlutterEngine.
Screen Shot 2020-02-25 at 10 34 02 AM

And why?

customer messaging question

Most helpful comment

Excuse me? But didn't @tinhpt38 already give you all the information you need?

If you create a new flutter project and setup firebase_messaging background handling the way it's explained in the readme, you should get the same error as he got.

What more do you want? Many people including me face this issue and its very frustrating.

All 12 comments

Hi @tinhpt38
does this answer help?
Thank you

Hi @iapicca !
I tried it but not help! I will take the same error!
Thanks you!

Hi @tinhpt38
can you please provide your flutter doctor -v ,
your flutter run --verbose, your pubspec.yaml
and if possible an updated reproducible minimal code sample.
Thank you

Same error!

Hi @tinhpt38
does @michaelcastro (thanks) solution works for you?
Thank you

Hi @tinhpt38
Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore
reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any
more information for us; we will reopen it right away!
Thanks for your contribution.

Excuse me? But didn't @tinhpt38 already give you all the information you need?

If you create a new flutter project and setup firebase_messaging background handling the way it's explained in the readme, you should get the same error as he got.

What more do you want? Many people including me face this issue and its very frustrating.

hi @lazylazyllama and @michaelcastro, for now, I still get an error when I using the background handle of firebase messing! I am going to waiting for any version upgrade to can fix it!

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationView extends StatefulWidget {
  @override
  NotificationState createState() {
    return NotificationState();
  }
}

class NotificationState extends State<NotificationView> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  FirebaseMessaging firebaseMessaging = new FirebaseMessaging();

  @override
  void initState() {
    initialize();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }

  showNotification() async {
    var androidPlatformChannelSpecifies = new AndroidNotificationDetails(
      "i-eX Driver",
      "i-eX Driver",
      "Test",
      importance: Importance.Max,
      priority: Priority.High,
      color: Colors.blue,
      autoCancel: true,
      largeIcon: DrawableResourceAndroidBitmap('@mipmap/engineer'),
      icon: '@mipmap/engineer',
    );
    var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails(presentAlert: true);
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifies, iOSPlatformChannelSpecifics);
    String data = "what to do with notification hereafetr kkklk....";
    firebaseMessaging.configure(
      onLaunch: (Map<String, dynamic> msg) {
        print(" onLaunch called ${(msg)}");
      },
      onResume: (Map<String, dynamic> msg) {
        print(" onResume called ${(msg)}");
      },
      onMessage: (Map<String, dynamic> msg) async {
        print(" onMessage called ${msg}");
        String val = '';
        msg.entries.map((v) {
          if (v.key == 'data') {
            v.value.entries.map((s) {
              if (s.key == 'body') val = s.value;
            }).toList();
          }
        }).toList();
        print(" onMessage called ${(val)}");
        await flutterLocalNotificationsPlugin.show(
            0, 'i-eX Driver', val, platformChannelSpecifics);
      },
    );
  }

  Future onDidReceiveLocalNotification(
      int id, String title, String body, String payload) async {
    showDialog(
      context: context,
      builder: (BuildContext context) => new CupertinoAlertDialog(
        title: new Text(title),
        content: new Text(body),
        actions: [
          CupertinoDialogAction(
            isDefaultAction: true,
            child: new Text('Ok'),
            onPressed: () async {
              Navigator.of(context, rootNavigator: true).pop();
            },
          )
        ],
      ),
    );
  }

  void initialize() async {
    AndroidInitializationSettings android = new AndroidInitializationSettings(
        '@mipmap/engineer'); //@mipmap/ic_launcher
    IOSInitializationSettings ios = new IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initSettings = new InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initSettings);
    firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, alert: true, badge: true));
    firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings setting) {
      print('IOS Setting Registed');
    });
    await firebaseMessaging.onTokenRefresh.listen((token) {
      print('FCM : ${token}');
    });

    showNotification();
  }
}

try this code


import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        // I've only had to add this line to make it work
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

And Main activity

import android.os.Bundle;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
  }
}
Was this page helpful?
0 / 5 - 0 ratings