Flutter图片通知后台及终止状态失效问题求助
Flutter iOS 图片通知后台/终止状态失效问题解决
问题现象
iOS平台下,Flutter应用前台运行时图片通知正常展示,但后台或应用终止状态下,图片无法正常显示。当前已实现将图片下载至Directory.systemTemp临时目录并在前台展示的逻辑。
现有实现代码
图片下载逻辑
if (Platform.isIOS) { if (message.data['image'] != null) { url = message.data['image']; var formatname = url.toString().split("."); var length = formatname.length - 1; var format = formatname[length]; log("message$format"); var formatlist = ['jpg', 'gif', 'png']; bigpicture = await DownloadUtil.downloadfiles(url, "bigpicture.${(format.isEmpty || formatlist.contains(format)) ? format : "jpg"}"); print(bigpicture); } else { bigpicture = " "; } }
通知展示代码
flutterLocalNotificationsPlugin.show( id, message.data["title"], message.data["body"], NotificationDetails( iOS: ios != null ? IOSNotificationDetails( attachments: message.data['image'] != null ? <IOSNotificationAttachment>[ IOSNotificationAttachment(bigpicture), ] : <IOSNotificationAttachment>[], ) : null, ), payload: json.encode(message.data));
使用的插件版本
- flutter_local_notifications: ^9.2.0
- firebase_messaging: ^11.2.5
推送Payload
{ "message": { "notification": { "title": "Welcome message", "body": "Firebase Nodification Success", "sound": "Default", "imageUrl": "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" }, "data": { "title": "Welcome message12", "body": "Firebase Nodification Success🙂", "click_action": "FLUTTER_NOTIFICATION_CLICK", "image": "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg", "status": "Received" } }, "registrationToken": "**fcmtoken**" }
Flutter环境信息
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.5, on macOS 12.5.1 21G83 darwin-arm, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [✓] Xcode - develop for iOS and macOS (Xcode 13.4) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] VS Code (version 1.69.2) [✓] Connected device (2 available) [✓] HTTP Host Availability • No issues found!
问题原因及解决方案
核心原因
iOS在后台/终止状态下,FCM推送的notification类型消息会由系统直接处理,不会触发Flutter端的消息回调,导致图片下载并添加附件的逻辑无法执行。同时Directory.systemTemp目录的文件在后台可能被系统清理,或系统无权限访问该目录文件用于通知附件。
解决步骤
改用纯Data消息推送
移除Payload中的notification字段,仅保留data字段。这样无论应用处于前台、后台还是终止状态,FCM都会将消息传递到Flutter的消息处理回调,确保图片下载逻辑正常执行。修改后的Payload示例:{ "message": { "data": { "title": "Welcome message12", "body": "Firebase Nodification Success🙂", "click_action": "FLUTTER_NOTIFICATION_CLICK", "image": "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg", "status": "Received" } }, "registrationToken": "**fcmtoken**" }更换图片存储目录
替换Directory.systemTemp为应用文档目录(需依赖path_provider插件),系统对该目录文件的访问权限更稳定,不会轻易清理。修改下载路径示例:import 'package:path_provider/path_provider.dart'; // 获取应用文档目录 final dir = await getApplicationDocumentsDirectory(); bigpicture = await DownloadUtil.downloadfiles(url, "${dir.path}/bigpicture.${(format.isEmpty || formatlist.contains(format)) ? format : "jpg"}");配置后台权限
- 在Xcode中开启应用的后台模式,勾选“Remote notifications”;
- 确保
Info.plist中UIBackgroundModes包含remote-notification。
验证附件合规性
确保下载的图片文件大小不超过iOS通知附件限制(通常≤10MB),且文件路径为应用有权访问的目录。
内容的提问来源于stack exchange,提问作者VIGNESH B
相关产品推荐
相关产品推荐

