Flutter FCM 图像未显示在通知中

问题描述 投票:0回答:1

有关如何在推送通知中显示我在 firebase 控制台中记录的图像的任何提示?它们正在工作,但是当我发送图像时,它不会出现在通知中。我正在使用 firebase_message 和 flutter_local_notifications。

当我发送通知时,它会出现,但图像不会出现。请记住,我将 URl 放入 firebaseConsole 中创建通知的图像中

Firebase配置:

class FireBaseNotifications {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
  final LocalNotifications _localNotifications;

  FireBaseNotifications._internal(this._localNotifications);
  static final FireBaseNotifications _singleton =
      FireBaseNotifications._internal(LocalNotifications());
  factory FireBaseNotifications() => _singleton;

  Future<void> initializeFirebasePushServices() async {
    await FirebaseMessaging.instance
        .setForegroundNotificationPresentationOptions(
            badge: true, sound: true, alert: true);

    FirebaseMessaging.onMessage.listen(
      (push) {
        RemoteNotification? notification = push.notification;
        AndroidNotification? androidNotification = push.notification?.android;
        AppleNotification? iosNotification = push.notification?.apple;

        if (notification != null && androidNotification != null) {
          _localNotifications.androidNotifications(
            notification: notification,
            androidNotification: androidNotification,
          );
        }
        if (notification != null && iosNotification != null) {
          _localNotifications.iosNotifications(
            notification: notification,
            iosNotification: iosNotification,
          );
        }
      },
    );
    if (Platform.isIOS) {
      FirebaseMessaging.onMessageOpenedApp.listen(
        (route) {
          if (route.data['goTo'] != null) {
            navigatorKey.currentState?.pushNamed(route.data['goTo']);
          }
        },
      );
    }
    if (Platform.isAndroid) {
      FirebaseMessaging.onMessageOpenedApp.listen(
        (route) {
          if (route.data['goTo'] != null) {
            navigatorKey.currentState?.pushNamed(route.data['goTo']);
          }
        },
      );
    }
  }

  Future<void> getTokenFirebase() async =>
      debugPrint(await FirebaseMessaging.instance.getToken());
}

Flutter 本地通知:

class LocalNotifications {
  late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  late AndroidNotificationChannel androidNotificationChannel;
  LocalNotifications() {
    androidNotificationChannel = const AndroidNotificationChannel(
      'high_importance_channel',
      'high_importance_notifications',
      description: 'channel used for very important notifications',
      importance: Importance.max,
    );
    _androidConfig().then(
      (value) {
        flutterLocalNotificationsPlugin = value;
        initializeNotifications();
      },
    );
    _iosConfig().then((value) {
      flutterLocalNotificationsPlugin = value;
      initializeNotifications();
    });
  }

  Future<FlutterLocalNotificationsPlugin> _androidConfig() async {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(androidNotificationChannel);
    return flutterLocalNotificationsPlugin;
  }

  Future<FlutterLocalNotificationsPlugin> _iosConfig() async {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    return flutterLocalNotificationsPlugin;
  }

  void initializeNotifications() {
    const android = AndroidInitializationSettings('@mipmap/ic_launcher');
    const ios = IOSInitializationSettings(
      // onDidReceiveLocalNotification: ,
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );
    flutterLocalNotificationsPlugin
        .initialize(const InitializationSettings(android: android, iOS: ios));
  }

  void androidNotifications({
    required RemoteNotification notification,
    required AndroidNotification? androidNotification,
  }) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
            androidNotificationChannel.id, androidNotificationChannel.name,
            channelDescription: androidNotificationChannel.description,
            icon: androidNotification?.smallIcon),
      ),
    );
  }

  void iosNotifications({
    required RemoteNotification notification,
    required AppleNotification? iosNotification,
  }) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      const NotificationDetails(
        iOS: IOSNotificationDetails(),
      ),
    );
  }
}
firebase flutter push-notification firebase-cloud-messaging
1个回答
0
投票

在 Firebase 控制台中,确保图像的 URL 或上传的图像大小小于 1MB。这适用于通过控制台上传的图像和从服务器有效负载中发送的图像 URL。 火力基地

© www.soinside.com 2019 - 2024. All rights reserved.