未显示 iOS 15 通信通知图片

问题描述

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

用户收到一位朋友的通信事件时,我希望显示通知包含该朋友的个人资料图片。就像新的 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
    • handledisplayName 设置正确
    • _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.

我缺少什么才能正确显示朋友的个人资料图片

谢谢

解决方法

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

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

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

  3. 最后,你的代码看起来不错,但你需要添加这个(注意:我只测试了 INSendMessageIntent

// Swift
incomingCommunicationIntent.setImage(image,forParameterNamed: \INStartCallIntent.sender)

// Objective-C
[incomingCommunicationIntent setImage:image forParameterNamed:"sender"];
,

对于需要更详细解决方案的任何人,@Dexwell 发布了great gist with Swift code

无论如何,提供的解决方案似乎从 iOS 15 Beta 4 开始工作(它不适用于 Beta 2,我无法尝试 Beta 3)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...