在 Flutter 中,我们如何使用 Firebase Messaging onBackgroundMessage 创建通知,使用 flutter_local_notifications?

In Flutter, how do we use Firebase Messaging onBackgroundMessage to create a notification, using flutter_local_notifications?

我们正在开发一个加密的聊天应用程序,我们在其中使用 Firebase Messaging 进行数据通知。在向用户显示实际通知之前,需要在收到数据通知后完成一些客户端逻辑。例如,phone 号码必须转换为本地联系人姓名。此翻译是通过查找全球可用的地图来完成的。

数据通知接收正常,onBackgroundMessage回调也被调用。但是,似乎无法从 onBackgroundMessage 函数访问任何类型的状态。例如,打印登录用户的phone号码returns null。 从 onMessage 回调打印这个相同的全局变量工作得很好。

来自 onMessage 的

运行 flutter_local_notifications 工作正常,但同样,来自 onBackgroundMessage 的 'no implementation could be found for the method .show()' 根本不起作用。目前,它声称 flutterLocalNotificationsPlugin 为 null,但事实并非如此。

在我们看来,一旦应用程序进入后台,onBackgroundMessage 就无法访问该应用程序提供的任何内容。必须做一些事情才能使某些 scope/context 可用于后台进程。目前,这主要是完整的 flutter_local_notifications 插件,以及将 phone 号码转换为姓名的本地联系人列表。

有人知道怎么做吗?

下面是部分代码:

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
final _chatRepository = ChatRepository();

Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
  if(message.containsKey('data')) {
    await _showNotification(message);
    return Future<void>.value();
  }
}

Future _showNotification(message) async {
  List<String> numbers = [];
  numbers.add(message['data']['sender']);
  var name = await _chatRepository.translatePhoneNumbersToChatName(numbers);
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'channel id', 'channel name', 'channel description',
      importance: Importance.Max, priority: Priority.High);
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    0,
    name,
    message['data']['body'],
    platformChannelSpecifics,
    payload: message['data']['body'],
  );
}

class NotificationHandler {
  final FirebaseMessaging fcm = FirebaseMessaging();
  StreamSubscription iosSubscription;
  String deviceToken = "";

  Future<void> initialize() async {
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    var initializationSettingsAndroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings(onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onClickNotification);

    fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        if(message.containsKey('data')) {
          print(message);
          _showNotification(message);
        }
      },
      onBackgroundMessage: Platform.isIOS
          ? null
          : backgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        if(message.containsKey('data')) {
          print(message);
          _showNotification(message);
        }
      },
      onResume: (Map<String, dynamic> message) async {
        if(message.containsKey('data')) {
          print(message);
          _showNotification(message);
        }
      },
    );
    _updateDeviceToken();
  }
.
.
.

Of course, the initialize above is called early on in the application lifecycle.

class NotificationHandler {
  static final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // make it a static field of the class
  // ...
}

Future _showNotification(message) async {
  // ...
  await NotificationHandler.flutterLocalNotificationsPlugin.show( // access it
    // ...
  );
}

希望这对你有用。

这个插件比我解释得更好,但碰巧背景是完全不同的 isolate/context,因此如果他们使用旧的(Flutter 12 之前的版本),它无法访问任何插件) API。 https://pub.dev/packages/android_alarm_manager#flutter-android-embedding-v1

嵌入 v1 要求您注册任何您想要从后台访问的插件。这样做可以使其 flutter_local_notifications 正常工作。

不幸的是,FCM 文档严重缺乏。