Flutter Firebase:后台未显示Heads up通知

7

当在Flutter应用程序中从云函数接收到FCM时,在iOS上通知会按预期出现横幅和系统托盘。但是,在Android上,当应用程序被终止或后台运行时,通知直接出现在系统托盘中而没有显示横幅。

我正在使用最新版本的Flutter和firebase消息传递插件,并在AndroidManifest.xml中设置了default_notification_channel_id,然后我使用本地通知插件创建了一个具有相同名称的Android通知渠道,这与我在AndroidManifest.xml中设置的名称相同,并为该通道设置了importance: Importance.max

我花了几天时间尝试显示Heads up通知,并阅读了所有相关文件和问题,但不幸的是仍未显示。 虽然我使用本地通知插件在前台显示Heads up通知没有问题。

最后,我使用本地通知插件在FirebaseMessaging.onBackgroundMessage上显示通知,以便获得Heads up通知,但由FCM发送的原始通知仍在通知托盘中。

非常感谢任何帮助。

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

有没有办法在Android 6上在后台显示Heads up通知? 如何设置默认FirebaseMessaging通道的重要性?


你找到任何解决方案了吗? - abdulec90
很遗憾,在Android 6上无法在后台显示FCM Heads up通知。 - Appoodeh
我只在一个Realme设备上遇到了这个问题,在其他设备上它都正常工作。 - abdulec90
1个回答

5

在AndroidManifest.xml中添加以下内容

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

在 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());
}

那么显示通知的代码在哪里?flutterLocalNotificationsPlugin.show。 - Harsha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接