【问题标题】:how to create a notification with small image using a String image path in Flutter?如何在 Flutter 中使用字符串图像路径创建带有小图像的通知?
【发布时间】:2021-06-05 05:00:03
【问题描述】:

我已经尝试观看了很多教程,这些教程展示了如何使用像这样的大图样式信息来实现大图的 natification

var bigPictureStyleInformation = BigPictureStyleInformation(
  FilePathAndroidBitmap(attachmentPicturePath),
  contentTitle: '<b>Attached Image</b>',
  htmlFormatContentTitle: true,
  summaryText: 'Test Image',
  htmlFormatSummaryText: true,
);

但我真正想要的是像这样在 Android 中显示带有小图像的通知

如果我有一个字符串图像路径,如何发出这样的通知?所以看来我必须从服务器下载图像,然后将其“附加”到通知中。如何做到这一点

【问题讨论】:

    标签: flutter flutter-local-notification


    【解决方案1】:

    对于未来的用户:

    如果您想获得与所附图像相同的图像,则应将largeIcon 传递给AndroidNotificationDetails

    代码是这样的(这个例子同时显示了大图标和大图像):

    这个例子来自flutter_local_notifications

      Future<String> _downloadAndSaveFile(String url, String fileName) async {
        final Directory directory = await getApplicationDocumentsDirectory();
        final String filePath = '${directory.path}/$fileName';
        final http.Response response = await http.get(Uri.parse(url));
        final File file = File(filePath);
        await file.writeAsBytes(response.bodyBytes);
        return filePath;
      }
    
    Future<void> _showBigPictureNotification() async {
        final String largeIconPath = await _downloadAndSaveFile(
            'https://via.placeholder.com/48x48', 'largeIcon');
        final String bigPicturePath = await _downloadAndSaveFile(
            'https://via.placeholder.com/400x800', 'bigPicture');
        final BigPictureStyleInformation bigPictureStyleInformation =
            BigPictureStyleInformation(FilePathAndroidBitmap(bigPicturePath),
                largeIcon: FilePathAndroidBitmap(largeIconPath),
                contentTitle: 'overridden <b>big</b> content title',
                htmlFormatContentTitle: true,
                summaryText: 'summary <i>text</i>',
                htmlFormatSummaryText: true);
        final AndroidNotificationDetails androidPlatformChannelSpecifics =
            AndroidNotificationDetails(
                'big text channel id', 'big text channel name',
                channelDescription: 'big text channel description',
                styleInformation: bigPictureStyleInformation);
        final NotificationDetails platformChannelSpecifics =
            NotificationDetails(android: androidPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.show(
            0, 'big text title', 'silent body', platformChannelSpecifics);
      }
    

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多