You need to enable JavaScript to run this app. iOS 15 Communication Notification 联系人头像无法显示问题求助
优惠活动
大模型
产品
解决方案
定价
更多

iOS 15 Communication Notification 联系人头像无法显示问题求助

你遇到的两个核心问题分别是:

  1. 直接用内存数据构造的INImage仅存在于你的App进程内存中,负责渲染通知的系统进程无法访问,触发了缓存不存在的报错
  2. INPerson参数匹配错误、交互捐赠时机不对,导致通知上下文的_displayName无法正常读取

解决方案按优先级排序如下:

1. 修正头像加载逻辑,避免使用内存构造INImage

负责渲染通信通知的是独立的系统进程,无法直接读取你的App内存中的图像数据,必须将头像写入本地临时目录,通过本地URL构造INImage,同时将头像作为通知附件挂载:

// 生成临时路径写入头像
let tempDir = FileManager.default.temporaryDirectory
let avatarPath = tempDir.appendingPathComponent(UUID().uuidString + ".png")
try! friend.profilePicture.cellImage.pngData()!.write(to: avatarPath)

// 构造专属头像附件,隐藏默认缩略图避免重复显示
let avatarAttachment = try! UNNotificationAttachment(
    identifier: "com.apple.UNNotificationAttachmentIdentifierCommunicationAvatar",
    url: avatarPath,
    options: [
        UNNotificationAttachmentOptionsThumbnailHiddenKey: true
    ]
)
// 附件必须加入通知的attachments数组
notification.attachments = [avatarAttachment]

// 用本地URL构造INImage,不要用pngData()直接构造
let image = INImage(url: avatarPath, width: 40, height: 40)

2. 修正INPerson参数匹配

你当前INPerson的suggestionType和personHandle类型不匹配,会导致系统无法正确识别联系人信息:

let intentFriend = INPerson(
  personHandle: INPersonHandle(value: friend.phoneNumber, type: .phoneNumber),
  nameComponents: friend.nameComponents,
  displayName: friend.localName,
  image: image,
  contactIdentifier: nil,
  customIdentifier: friend.userId, // 必须填唯一用户标识,不可留空
  isMe: false,
  suggestionType: .phoneNumber // 和personHandle的type保持一致,不要用.instantMessageAddress
)

3. 调整交互捐赠时机,确保捐赠完成后再更新通知

你当前直接调用donate(completion: nil)后立刻更新通知,可能交互还未完成捐赠,系统未读取到上下文就触发了通知渲染,必须把更新逻辑放到捐赠的回调中:

let interaction = INInteraction(intent: incomingCommunicationIntent, response: nil)
interaction.direction = .incoming
interaction.donate { error in
    guard error == nil else {
        print("交互捐赠失败: \(error!)")
        return
    }
    do {
        let updatedContent = try notification.updating(from: incomingCommunicationIntent)
        // 此处再触发本地通知,或在通知服务扩展中返回updatedContent
    } catch {
        print("通知更新失败: \(error)")
    }
}

4. 额外适配注意事项

  • 推送通知场景下,上述所有逻辑必须写在Notification Service Extension中,不可放在主App处理,否则杀进程/后台状态下逻辑不会执行
  • 主App和Intent扩展、通知服务扩展的部署目标必须统一为iOS 15及以上版本
  • 主App的Info.plist中NSUserActivityTypes数组需要额外添加INStartCallIntent项
  • 提前注册通知Category,options需要包含.allowAnnouncement

内容的提问来源于stack exchange,提问作者Antoine Baché

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 17:12:02