android - Flutter Firebase : Heads up notification not showing on background

标签 android firebase flutter push-notification heads-up-notifications

在 Flutter 应用程序中从云功能接收 FCM 时,在 iOS 上,通知会按预期显示在系统托盘中并带有横幅。但是,在 Android 上,当应用程序终止或在后台时,通知会直接显示在系统托盘中而不显示横幅。
我正在使用最新版本的 flutter 和火力消息插件,我设置default_notification_channel_idAndroidManifest.xml ,然后我使用本地通知插件创建了一个与我在 AndroidManifest.xml 中设置的名称相同的 Android 通知 channel 我确实设置了importance: Importance.max为 channel 。
我花了几天时间尝试显示提醒通知,我将所有文档和相关问题都涂红了,但不幸的是它仍然没有显示。
虽然我使用本地通知插件在前台显示提示通知,但没有问题。
最后,我使用本地通知插件在 FirebaseMessaging.onBackgroundMessage 上显示通知所以我收到了 Heads up 通知,但 FCM 发送的原始通知仍在通知托盘中。
我很感激任何帮助。
编辑:
问题是我用于测试的Android版本,
通知 channel 是特定于 Android 8 或更高版本的概念,这就是为什么 API 文档声明创建 channel 的方法仅适用于那些版本的 Android
有没有办法在 Android 6 的背景上显示抬头通知?
我怎样才能默认 FirebaseMessaging channel 重要性?

最佳答案

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

关于android - Flutter Firebase : Heads up notification not showing on background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67401303/

相关文章:

android - 如何在我的 Android 项目中使用 zxing 核心作为库?

android - Kivy:跨平台通知图标

javascript - 无法在 firebase 上上传数据?

android - 在 firebase 分析调试 View 中看不到事件

Flutter - 如何在 ListView 中居中小部件

flutter 换行文本而不是溢出

android - 二维码中的电话号码

android - 由 Subjects 组成的 Observable transformer

flutter - 如何在flutter中将Widget传递给const Constructor?

Firebase Firestore : How to monitor read document count by collection?