Flutter Firebase: Heads up notification not showing on background
Asked Answered
H

1

8

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?

Hesitate answered 5/5, 2021 at 12:28 Comment(3)
Did you find any solution for it ?Parameter
No, Unfortunately There Is no way to show FCM Heads up notification on background on Android 6Hesitate
i am facing this issue only on on of realme device, it is working fine other devicesParameter
U
6

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());
}
Unmask answered 18/8, 2021 at 22:9 Comment(2)
So where is the code to show the notification? flutterLocalNotificationsPlugin.showSycophancy
but it is using another package, how to show without package when on background?Axe

© 2022 - 2024 — McMap. All rights reserved.