ahz
ahz

Reputation: 53

Flutter Firebase Background Messaging

I want to use onBackgroundMessage, so my idea is if notification received when app minimized or killed I try to play something audio file from asset with AudioPlayer package, but I got error like this when flutter runs :

E/flutter (31608): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value E/flutter (31608): #0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler

However app still work when flutter run although the error above appears. When I try send notification, app received the notification on header, but I got another messages like this

A background message could not be handled in Dart as no onBackgroundMessage handler has been registered. Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

I never changes anything in android folder or ios folder. please help me to solve this problem

This is my dart code :

  FirebaseMessaging messaging = FirebaseMessaging.instance;
  AudioCache audioCache = AudioCache();
  AudioPlayer audioPlayer = AudioPlayer();

  Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    await Firebase.initializeApp();
    print("Background message service");
    audioPlayer = await audioCache.play('dangeronehour.mp3');
  }

  void initMessaging() async {
    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    messaging.getToken().then((value) => print(value));
    print('${settings.authorizationStatus}');
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initMessaging();
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Message in the foreground!');
      print('Message data: ${message.data}');
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      print('App opened');
      audioPlayer = await audioCache.play('dangeronehour.mp3');
    });
  }

Firebase packages I used

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  firebase_core: ^1.0.3
  firebase_messaging: ^9.1.1
  flutter_local_notifications:
  audioplayers:

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 2.1.0-13.0.pre.586, on Mac OS X 10.15.2 19C57 darwin-x64, locale en)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS
    ✗ Xcode 11.6.0 out of date (12.0.1 is recommended).
      Download the latest version or update via the Mac App Store.
    ! CocoaPods 1.8.4 out of date (1.10.0 is recommended).
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device (2 available)

Flutter version

Flutter 2.1.0-13.0.pre.586 • channel master • https://github.com/flutter/flutter.git
Framework • revision fbf3c4e41c (26 hours ago) • 2021-04-12 17:42:25 -0700
Engine • revision 8863afff16
Tools • Dart 2.13.0 (build 2.13.0-222.0.dev)

Upvotes: 1

Views: 2968

Answers (1)

Celt K. B.
Celt K. B.

Reputation: 789

Put this in AndroidManifest.xml that you will find in the android folder:

   <intent-filter>
         <action android:name="FLUTTER_NOTIFICATION_CLICK" />
         <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>

You can put this at the at the end of the activity, but before you close the activity

Upvotes: 1

Related Questions