3

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

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

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

最后我使用了本地通知插件来显示通知,FirebaseMessaging.onBackgroundMessage所以我得到了抬头通知,但 FCM 发送的原始通知仍在通知托盘中。

我很感激任何帮助。

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

有没有办法在 Android 6 的背景上显示抬头通知?我怎样才能默认 FirebaseMessaging 渠道重要性?

4

1 回答 1

1

在 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());
}
于 2021-08-18T22:09:04.190 回答