Flutter Firebase: Heads up notification not showing on background

1,038

add this in AndroidManifest.xml

<meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="badrustuts" />

in main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

///receive message when app is in background
Future<void> backgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
}

///create channel
AndroidNotificationChannel channel = const AndroidNotificationChannel(
  'badrustuts', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.high,
);

/// initialize the [FlutterLocalNotificationsPlugin] package.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  /// background messaging handler
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  runApp(MyApp());
}
Share:
1,038
Appoodeh
Author by

Appoodeh

Updated on December 01, 2022

Comments

  • Appoodeh
    Appoodeh over 1 year

    When receiving an FCM from cloud functions in a Flutter app, on iOS the notification appears with a banner and in the system tray as expected. However, on Android the notification appears straight in the system tray without displaying a banner, when the app is terminated or in background.

    I am using latest versions of flutter and firebase messaging plugin, and I set the default_notification_channel_id in the AndroidManifest.xml, then I used the Local Notifications Plugin to create an Android Notification Channel with the same name that I did set in AndroidManifest.xml and I did set importance: Importance.max for the channel.

    I spent a few days trying show Heads up notifications and I red all of the documentations and related issues, but unfortunately it is still not showing. Although I am using the Local Notifications Plugin to show Heads up notifications on foreground with no problems.

    At last I used the Local Notifications Plugin to show the notification on FirebaseMessaging.onBackgroundMessage so I get the Heads up notification, but the original notification that sent by FCM is still in the notification tray.

    I appreciate any help.

    Edit: The problem was the Android version that I am using for testing, Notification channels is a concept that is specific Android 8 or newer, which is why the API docs state that the method to create channels is only for those versions of Android

    Is there any way to show Heads up notification on background on Android 6? How can I the default FirebaseMessaging channel importance?