ios - iOS 10 推送通知中的媒体附件
<p><p>我正在为在 iOS 10 中向我的推送通知添加图像而苦苦挣扎。</p>
<p>我添加了通知服务扩展,并使用了以下代码:</p>
<pre><code> override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
let options =
if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
self.bestAttemptContent?.attachments =
}
}
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
</code></pre>
<p>我从下面的第一个答案中得到了这段代码。 </p>
<p>我现在遇到的问题是收到了通知,有一个短暂的延迟,表明下载一定在进行,但没有显示附件。</p>
<p>我假设正在调用 <code>serviceExtensionTimeWillExpire()</code> 并且只显示 <code>bestAttempt</code></p>
<p>非常感谢任何帮助。</p>
<p>我相信我的 APNs 负载配置正确,我相信:</p>
<pre><code>apns: {
aps: {
alert: {
title: "Title",
subtitle: "Subtitle",
body: "Body"
},
"mutable-content": 1
},
"image-url": "https://helloworld.com/image.png"
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您必须从通知的用户信息中提取 url,然后下载图像并为附件提供文件 url。尝试类似的东西:</p>
<pre><code>if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
let options =
if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
self.bestAttemptContent.attachments =
}
}
self.contentHandler(self.bestAttemptContent)
}.resume()
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - iOS 10 推送通知中的媒体附件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39399154/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39399154/
</a>
</p>
页:
[1]