Flutter Firebase:抬头通知未显示在后台

App*_*deh 8 android push-notification firebase heads-up-notifications flutter

当 Flutter 应用程序中的云功能收到 FCM 时,在 iOS 上,通知会按预期显示在系统托盘中并带有横幅。然而,在 Android 上,当应用程序终止或在后台时,通知会直接显示在系统托盘中,而不显示横幅。

我正在使用最新版本的 flutter 和 firebase 消息传递插件,并且我 default_notification_channel_id在 中设置了 ,然后我使用本地通知插件创建了一个 Android 通知通道,其名称与我在其中设置的名称以及为该通道设置的AndroidManifest.xml名称相同。AndroidManifest.xmlimportance: Importance.max

我花了几天时间尝试显示提醒通知,并红色了所有文档和相关问题,但不幸的是它仍然没有显示。尽管我使用本地通知插件在前台显示提示通知,没有任何问题。

最后,我使用本地通知插件来显示通知,FirebaseMessaging.onBackgroundMessage因此我收到了提醒通知,但 FCM 发送的原始通知仍在通知托盘中。

我很感激任何帮助。

编辑: 问题是我用于测试的 Android 版本,通知通道是特定于 Android 8 或更高版本的概念,这就是为什么 API 文档声明创建通道的方法仅适用于那些版本的 Android

有什么方法可以在 Android 6 上在后台显示抬头通知吗?我如何确定默认的 FirebaseMessaging 通道重要性?

小智 5

在 AndroidManifest.xml 中添加此内容

<meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="badrustuts" />
Run Code Online (Sandbox Code Playgroud)

在 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());
}
Run Code Online (Sandbox Code Playgroud)