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
相关产品推荐
相关产品推荐

