【问题标题】:Flutter Firebase: Heads up notification not showing on backgroundFlutter Firebase:抬头通知未在后台显示
【发布时间】:2021-07-27 18:37:33
【问题描述】:

当从 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 渠道重要性?

【问题讨论】:

  • 你找到解决办法了吗?
  • 不,很遗憾,在 Android 6 上无法在后台显示 FCM Heads up 通知
  • 我只在 realme 设备上遇到这个问题,其他设备运行良好

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


【解决方案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());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2017-02-02
    相关资源
    最近更新 更多