使用 Firebase Messaging 打开应用程序时 Flutter 显示通知

Flutter Show Notification when app is open with Firebase Messaging

应用程序收到我在后台或关闭模式下发送的所有通知,但我还想在用户玩应用程序时打开应用程序时显示通知。

      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        FlutterRingtonePlayer.playNotification();
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");

      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        // TODO optional
      },

如果你想在系统托盘中显示通知,就像应用程序在后台时一样,你可以使用包 flutter_local_notifications

这样,当您通过 onMessage 收到通知时,您可以使用如下方式:

AndroidNotificationDetails notificationAndroidSpecifics =
    AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        importance: Importance.Max,
        priority: Priority.High,
        groupKey: groupKey);

NotificationDetails notificationPlatformSpecifics =
    NotificationDetails(notificationAndroidSpecifics, null);

await flutterLocalNotificationsPlugin.show(
    1,
    'Jeff Chang',
    'Please join us to celebrate the...',
    notificationPlatformSpecifics);

查看他们的文档以获取更多示例!