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

iOS UserNotifications带图片附件本地富通知 禁止图片展开仅显示缩略图问询

问题解答

该需求完全可以实现,两种实现方案如下:

方案1:仅限制图片不展开,保留缩略图展示

通过调整通知附件的配置参数即可实现,附件本身会在通知展开时隐藏,但缩略图会正常展示在通知条的固定位置:

  • 配置UNNotificationAttachment的选项时,添加UNNotificationAttachmentOptionsHidden为true,同时保留UNNotificationAttachmentOptionsThumbnailHidden为false
  • 代码示例:
// 图片本地路径替换为你自己的资源路径
let imageURL = Bundle.main.url(forResource: "notificationImage", withExtension: "png")!
let attachmentOptions = [
    UNNotificationAttachmentOptionsThumbnailHidden: false,
    UNNotificationAttachmentOptionsHidden: true
] as [String : Any]
if let attachment = try? UNNotificationAttachment(identifier: "noticeImg", url: imageURL, options: attachmentOptions) {
    notificationContent.attachments = [attachment]
}

方案2:完全禁止通知展开

如果需要彻底关闭通知的长按/Force Touch展开能力,可以通过配置通知分类实现:

  • 创建通知分类时options参数留空,不添加任何允许展开的属性
  • 代码示例:
// 注册无展开能力的通知分类
let noExpandCategory = UNNotificationCategory(identifier: "noExpand", actions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([noExpandCategory])
// 发送通知时绑定对应分类ID
notificationContent.categoryIdentifier = "noExpand"

注意:该方案会同时禁用通知展开后的所有自定义内容、交互动作,适合不需要额外通知操作的场景

补充说明

两种方案可以叠加使用,达到双重限制的效果;如果用户在系统设置中关闭了App的通知预览权限,缩略图也会被系统隐藏,该权限属于用户可控范围,App侧无法强制修改。

内容的提问来源于stack exchange,提问作者Calamitosis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:57:03