未显示 iOS 15 通信通知图片 [英] iOS 15 Communication Notification picture not shown
问题描述
我一直在尝试将我的(本地和推送)通知更新为通信通知.
I've been trying to update my (local & push) notifications to Communication notifications.
当用户收到一位朋友的通信事件时,我希望显示的通知包含该朋友的个人资料图片.就像新的 iMessage 应用一样.
When a user receives a communication event from one of their friend, I want the displayed notification to include the friend's profile picture. Just like the new iMessage app does.
在观看了专门的 WWDC2021 会议后,我在我的 SwiftUI 应用中添加了一个 Siri Intent:
After watching the dedicated WWDC2021 session, I added a Siri Intent to my SwiftUI app:
// I correctly added the StartCallIntent to both my app's Info.plist and the Siri Intent extension.
<array>
<string>INStartCallIntent</string>
</array>
并尝试使用以下内容更新我的通知流程:
and tried to update my notification flow with the following:
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
以显示为推送通知或本地通知.
The returned UNNotificationContent
is then handled to be displayed either as a push notification or a local one.
虽然上面的代码没有崩溃并且似乎是/work/,但通知看起来没有任何不同.使用调试器查看,_UNNotificationCommunicationContext
已初始化,但是:
While the above code doesn't crash and seems to /work/, the notification doesn't look any different.
Looking with a debugger, the _UNNotificationCommunicationContext
is initialized, but:
_displayName
为零_recipients
为空(如预期)_contentURL
为零_attachments
为空sender
设置为UNNotificationContact
句柄
&displayName
设置正确_handleType
似乎也设置正确
_displayName
is nil_recipients
is empty (as expected)_contentURL
is nil_attachments
is emptysender
is set to aUNNotificationContact
on whichhandle
&displayName
are correctly set_handleType
seems correctly set too
在应用的日志中,我可以看到:
In the app's logs, I can see:
[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.
我缺少什么才能正确显示朋友的个人资料图片?
What am I missing to correctly display the friend's profile picture?
谢谢
推荐答案
不幸的是,Apple 在记录通信通知方面做得并不好,他们的 WWDC 视频遗漏了大量非常重要的细节,但有一些事情:
Unfortunately Apple did not do a great job at documenting communication notifications, their WWDC video leaves a ton of very important details out, but there are a few things:
在您的通知服务扩展的
Info.plist
中,您需要添加以下内容:NSExtension
->NSExtensionAttributes
(dict) ->IntentsSupported
(数组)->INSendMessageIntent
或INStartCallIntent
.
In your Notification Service Extension's
Info.plist
you need to add this:NSExtension
->NSExtensionAttributes
(dict) ->IntentsSupported
(array) ->INSendMessageIntent
orINStartCallIntent
.
确保您启用了通信通知";主要应用目标的能力.
Make sure you enabled the "Communication Notifications" capability on your main app target.
最后,你的代码看起来不错,但你需要添加这个(注意:我只测试了
INSendMessageIntent
)Finally, your code looks right but you need to add this (note: I have only tested this for
INSendMessageIntent
)// Swift incomingCommunicationIntent.setImage(image, forParameterNamed: \.sender) // Objective-C [incomingCommunicationIntent setImage:image forParameterNamed:"sender"];
这篇关于未显示 iOS 15 通信通知图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!