swift - 未显示 iOS 15 通信通知图片

标签 swift apple-push-notifications uilocalnotification wwdc ios15

我一直在尝试将我的(本地和推送)通知更新为通信通知。
当用户从他们的一位 friend 收到通信事件时,我希望显示的通知包含 friend 的个人资料图片。就像新的 iMessage 应用程序一样。
在观看了专门的 WWDC2021 session 后,我在我的 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.
    
    我缺少什么才能正确显示 friend 的个人资料图片?
    谢谢

    最佳答案

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

  • 在您的通知服务扩展的 Info.plist 中你需要添加这个:NSExtension -> NSExtensionAttributes (字典)-> IntentsSupported (数组)-> INSendMessageIntentINStartCallIntent .
  • 确保您在主应用程序目标上启用了“通信通知”功能。
  • 最后,你的代码看起来不错,但你需要添加这个(注意:我只测试了 INSendMessageIntent )
  • // Swift
    incomingCommunicationIntent.setImage(image, forParameterNamed: \.sender)
    
    // Objective-C
    [incomingCommunicationIntent setImage:image forParameterNamed:"sender"];
    

    关于swift - 未显示 iOS 15 通信通知图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68198452/

    相关文章:

    uitableview - 滚动到表格 View 的最后一行只会滚动一半

    swift - RxSwift 从一个 Observable 更新多个变量

    ios - 数组在错误的位置返回值

    iOS 应用程序被杀死并重新启动 显示我最后的 VC

    ios - 在 iOS 上设置 Firebase 通知时出错

    iphone - 在本地通知后呈现 UIViewController

    ios - 查明用户是否已接受接收推送通知?

    ios - iOS 推送通知的系统声音

    iOS通知声音中断

    ios - UILocalNotifications 触发时没有声音