You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 05:32:35