iOS富通知中WebP格式图片无法显示的技术求助
iOS富通知无扩展名WebP图片无法显示的解决方案
核心问题分析
iOS通知服务扩展(Service Extension)和cleverPush SDK默认可能依赖文件扩展名识别图片类型,无扩展名的WebP资源会被判定为非支持媒体格式,导致无法显示。以下是针对性解决步骤:
1. 配置通知扩展支持WebP MIME类型
在Service Extension的Info.plist中添加WebP的媒体类型支持,让系统识别该格式:
<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionSupportedMediaTypes</key> <array> <string>image/png</string> <string>image/jpeg</string> <string>image/webp</string> </array> </dict> <key>NSExtensionPointIdentifier</key> <string>com.apple.usernotifications.service</string> </dict>
2. 拦截WebP资源并转换为iOS原生支持格式
如果cleverPush SDK未直接处理无扩展名WebP,可在Service Extension中手动下载、转换为PNG/JPG后附加到通知:
import UserNotifications import SDWebImage import SDWebImageWebPCoder class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) guard let content = bestAttemptContent, let mediaUrlString = content.userInfo["media-url"] as? String, let mediaUrl = URL(string: mediaUrlString) else { contentHandler(request.content) return } // 通过URL参数判断是否为WebP格式(根据实际场景调整) let isWebP = mediaUrl.query?.contains("format=webp") ?? false if isWebP { SDWebImageDownloader.shared.downloadImage(with: mediaUrl) { [weak self] image, data, error, finished in guard let self = self, let image = image else { contentHandler(content) return } // 转换为PNG数据 guard let pngData = image.pngData() else { contentHandler(content) return } // 保存到临时目录 let tempDir = URL(fileURLWithPath: NSTemporaryDirectory()) let tempFile = tempDir.appendingPathComponent("converted_image.png") do { try pngData.write(to: tempFile) // 创建通知附件 let attachment = try UNNotificationAttachment(identifier: "webp_attachment", url: tempFile, options: nil) content.attachments = [attachment] self.contentHandler?(content) } catch { contentHandler(content) } } } else { // 非WebP资源交给cleverPush默认处理 contentHandler(content) } } override func serviceExtensionTimeWillExpire() { // 超时 fallback if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } }
3. 依赖配置说明
- 在Service Extension的Target中添加
SDWebImage和SDWebImageWebPCoder依赖(可通过CocoaPods或Swift Package Manager引入); - 确保推送Payload包含
mutable-content: 1,否则Service Extension不会被触发。
4. 验证cleverPush SDK配置
检查cleverPush后台推送配置:
- 确认媒体URL的
format参数正确标记为webp; - 查看SDK文档,确认是否支持通过Payload指定媒体类型(如添加
media-type: image/webp字段)。
内容的提问来源于stack exchange,提问作者Arslan Asim
相关产品推荐
相关产品推荐

