我正试图让通信通知与我设置的远程推送通知一起工作,特别是这样我就可以让用户头像出现在通知中。我已经成功地为我的应用程序设置了一个通知服务扩展,我已经验证了它的有效性(断点在
NotificationService
类,我可以修改标题),并通过以下方式
guide
,我最终得到了这段代码:
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent {
contentHandler(bestAttemptContent)
}
let personHandle = INPersonHandle(value: "senderID", type: .unknown)
let person = INPerson(
personHandle: personHandle,
nameComponents: .init(givenName: "senderName", familyName: "family name"),
displayName: "senderName",
image: nil,
contactIdentifier: nil,
customIdentifier: "some-unique-id"
)
let intent = INSendMessageIntent(
recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: "some content",
speakableGroupName: nil,
conversationIdentifier: "unique-user-id-conv",
serviceName: nil,
sender: person,
attachments: nil
)
let interaction = INInteraction(intent: intent, response: nil)
interaction.direction = .incoming
interaction.donate(completion: nil)
do {
let updatedContent = try request.content.updating(from: intent)
let mutableBestAttemptContent = (updatedContent.mutableCopy() as? UNMutableNotificationContent)!
mutableBestAttemptContent.userInfo = request.content.userInfo
contentHandler(mutableBestAttemptContent)
} catch {
}
}
当我收到通知时,这段代码会运行,我已经用断点进行了验证
contentHandler(mutableBestAttemptContent)
被调用,并且意图是捐赠的,没有错误。
现在,我省略了添加图像,因为我想首先得到一个最小的示例,其中
person
显示(尽管我也用硬编码测试过
INImage(url: validURL)
没有成功),但此代码不起作用。
此外,我还有以下几点:
-
通知服务扩展信息.plist文件包含以下内容:
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IntentsSupported</key>
<array>
<string>INSendMessageIntent</string>
</array>
</dict>
...
</dict>
</dict>
-
主应用程序目标在其Info.plist中有以下内容
<dict>
<key>NSUserActivityTypes</key>
<array>
<string>INSendMessageIntent</string>
</array>
...
</dict>
-
主应用程序目标中添加了“通信通知”功能,以及“推送通知”、“背景模式”(选中“远程通知”)和“Siri”。
-
可能无关紧要,但我在某个地方看到,服务扩展的捆绑包ID必须以主应用程序目标的捆绑包标识为前缀,情况也是如此。
我有点不明白为什么这不起作用,而且关于这件事的文档少得令人震惊,所以我甚至不知道如何调试它。有人有什么想法吗?