【问题标题】:iOS 15 Communication Notification picture not showniOS 15 通讯通知图片未显示
【发布时间】:2021-09-12 19:55:13
【问题描述】:

我一直在尝试将我的(本地和推送)通知更新为通信通知。

当用户从他们的一位朋友那里收到通信事件时,我希望显示的通知包含该朋友的个人资料图片。就像新的 iMessage 应用一样。

在观看了专门的 WWDC2021 会议后,我在我的 SwiftUI 应用中添加了一个 Siri Intent:

// I correctly added the StartCallIntent to both my app's Info.plist and the Siri Intent extension.
<array>
    <string>INStartCallIntent</string>
</array>

并尝试使用以下内容更新我的通知流程:

let image = INImage(imageData: friend.profilePicture.cellImage.pngData()!)
let intentFriend = INPerson(
  personHandle: INPersonHandle(value: friend.phoneNumber, type: .phoneNumber),
  nameComponents: friend.nameComponents,
  displayName: friend.localName,
  image: image,
  contactIdentifier: nil,
  customIdentifier: nil,
  isMe: false,
  suggestionType: .instantMessageAddress
)

let incomingCommunicationIntent = INStartCallIntent(
  callRecordFilter: nil,
  callRecordToCallBack: nil,
  audioRoute: .unknown,
  destinationType: .normal,
  contacts: [intentFriend],
  callCapability: .audioCall
)

let interaction = INInteraction(intent: incomingCommunicationIntent, response: nil)
interaction.direction = .incoming
interaction.donate(completion: nil)

do {
  let result = try notification.updating(from: incomingCommunicationIntent)
  return result
} catch let error {
  print("-- \(error)")
  // TODO: Handle error here
}

return notification

然后处理返回的UNNotificationContent 以显示为推送通知或本地通知。

虽然上面的代码没有崩溃并且似乎 /work/,但通知看起来并没有什么不同。 使用调试器查看,_UNNotificationCommunicationContext 已初始化,但是:

  • _displayName 为零
  • _recipients 为空(如预期)
  • _contentURL 为零
  • _attachments 为空
  • sender 设置为 UNNotificationContact
    • handle & displayName 设置正确
    • _handleType 似乎也设置正确

在应用程序的日志中,我可以看到:

[Intents] -[INCache cacheableObjectForIdentifier:] Unable to find cacheable object with identifier EDB29166-E46A-CF23-AB27-8B61F763A039 in cache.
[Intents] -[INCache cacheableObjectForIdentifier:] Unable to find cacheable object with identifier intents-remote-image-proxy:?proxyIdentifier=EDB29166-E46A-CF23-AB27-8B61F763A039.png&storageServiceIdentifier=com.apple.Intents.INImageServiceConnection in cache.

要正确显示朋友的个人资料图片,我缺少什么?

谢谢

【问题讨论】:

    标签: swift apple-push-notifications uilocalnotification wwdc ios15


    【解决方案1】:

    不幸的是,Apple 在记录通信通知方面做得不好,他们的 WWDC 视频留下了大量非常重要的细节,但有几件事:

    1. 在通知服务扩展的Info.plist 中,您需要添加以下内容: NSExtension -> NSExtensionAttributes (dict) -> IntentsSupported (array) -> INSendMessageIntentINStartCallIntent.

    2. 确保您在主应用目标上启用了“通信通知”功能。

    3. 最后,您的代码看起来正确,但您需要添加此代码(注意:我仅针对 INSendMessageIntent 测试过此代码)

    // Swift
    incomingCommunicationIntent.setImage(image, forParameterNamed: \.sender)
    
    // Objective-C
    [incomingCommunicationIntent setImage:image forParameterNamed:"sender"];
    

    【讨论】:

    • 非常感谢您的帮助! Swift 代码有一个小错误,我猜你想简单地输入\.sender 另外,我遇​​到了问题,因为我运行的是 iOS 15 Beta 2。当我升级到 Beta 4 时,一切都开始工作了。出于好奇,您是如何找到此解决方案的?试验/猜测?
    • 谢谢 nightsd01!我还发现,在 iOS 15 Beta 6 上进行测试时,使用图像数据初始化 INImage 效果很好,但在使用远程图像 url 初始化时效果不佳。
    • @owjsub 如果您正在调度异步,代码将退出函数并且不会执行完成。确保发送同步。
    • 我已经完成了上述所有操作,但仍然出现“无法在缓存中找到带有标识符的对象”错误。
    • @johnnykehr 这是great gist with a Swift example@Dexwell 还值得注意的是,上述解决方案似乎从 iOS 15 Beta 4
    猜你喜欢
    • 2021-11-16
    • 2022-10-22
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多