带有Xcode,Swift3的iOS Rich Push通知但无法获取图像
<p><p>我正在尝试使用 Xcode、Swift3 创建 iOS 富推送通知。
我已经使用 php 的 curl 命令确定了推送通知(主题、正文),但我无法创建引用 <a href="https://qiita.com/nnsnodnb/items/22b989abb8fcbad2e34d" rel="noreferrer noopener nofollow">in this document</a> 的丰富推送通知.</p>
<p>我添加了这样的通知服务扩展:<code>「文件」→「新建」→「目标...」→「通知服务扩展」</code>,我也在<code>中添加了“'mutable_content' : 真」</code> curl 命令。</p>
<p>然后运行但不调用<code>「class NotificationService: UNNotificationServiceExtension」</code>所以无法查看推送通知图片。</p>
<p>下面是我的代码</p>
<pre><code>import UserNotifications
class NotificationService: UNNotificationServiceExtension {
let imageKey = AnyHashable("gcm.notification.image_url")
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let imageUrl = request.content.userInfo as? String {
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: URL(string: imageUrl)!, completionHandler: { (data, response, error) in
if let data = data {
do {
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("push.png")
try data.write(to: writePath)
guard let wself = self else {
return
}
if let bestAttemptContent = wself.bestAttemptContent {
let attachment = try UNNotificationAttachment(identifier: "nnsnodnb_demo", url: writePath, options: nil)
bestAttemptContent.attachments =
contentHandler(bestAttemptContent)
}
} catch let error as NSError {
print(error.localizedDescription)
guard let wself = self else {
return
}
if let bestAttemptContent = wself.bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
} else if let error = error {
print(error.localizedDescription)
}
})
task.resume()
} else {
if let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent =bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>引用</strong>:<a href="https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications" rel="noreferrer noopener nofollow">https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications</a> </p>
<p>我已经这样做了,希望它对 <strong>GIF</strong> 图像有所帮助,您可以将扩展名更改为 <strong>.png</strong>。</p>
<ol>
<li>确保在 APNS 有效负载 <code>attachment-url</code> 中为图像提供。</li>
<li>检查 <strong>App transport security</strong> 键,以防图像 url 从 <code>http://...</code></li> 开始
<li>您的图片应小于 <strong>~200px</strong>。对我来说,它无法超越(HIT 和 TRIAL)。</li>
</ol>
<p><strong>代码:</strong></p>
<pre><code>import UserNotifications
import SDWebImage
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
func failEarly() {
contentHandler(request.content)
}
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()
}
guard let attachmentURL = content.userInfo["attachment-url"] as? String else {
return failEarly()
}
SDWebImageDownloader.shared().downloadImage(with: URL(string: attachmentURL)!,
options: SDWebImageDownloaderOptions.continueInBackground,
progress: nil) { (image, data, error, flag) in
guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image.gif",
data: data! as NSData,
options: nil) else { return failEarly() }
content.attachments =
contentHandler(content.copy() as! UNNotificationContent)
if let bestAttemptContent = self.bestAttemptContent {
bestAttemptContent.title = "\(bestAttemptContent.title) "
contentHandler(bestAttemptContent)
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent =bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationAttachment {
static func create(imageFileIdentifier: String, data: NSData, options: ?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL!, options: [])
let imageAttachment = try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL!, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}
</code></pre></p>
<p style="font-size: 20px;">关于带有Xcode,Swift3的iOS Rich Push通知但无法获取图像,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48882711/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48882711/
</a>
</p>
页:
[1]