使用Xcode,Swift3的iOS Rich Push通知但无法获取图像

我正在尝试使用Xcode,Swift3创建iOS Rich Push通知。 我已经使用php的curl命令确保推送通知(主题,正文),但我无法创建本文档中提到的Rich Push Notifications。

我添加了这样的通知服务扩展: 「 File 」→「 New 」→「 Target... 」→「 Notification Service Extension 」 ,还添加了「'mutable_content': True」curl命令。

然后运行但不调用「class NotificationService: UNNotificationServiceExtension」因此无法查看推送通知图像。

以下我的代码

 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[imageKey] as? String { let session = URLSession(configuration: URLSessionConfiguration.default) let task = session.dataTask(with: URL(string: imageUrl)!, completionHandler: { [weak self] (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 = [attachment] 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) } } } 

参考 : https : //www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications

我已经做了这样的希望它有助于GIF图像,你可以改变扩展为.png

  1. 确保在APNS中有效载荷attachment-url即将出现在图像中。
  2. 如果图像url从http://...开始检查App传输安全密钥http://...
  3. 你的图像应该在~200px以下 。 对我而言,它不会超越(HIT和TRIAL)。

码:

 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 = [attachment] contentHandler(content.copy() as! UNNotificationContent) if let bestAttemptContent = self.bestAttemptContent { bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" contentHandler(bestAttemptContent) } } } override func serviceExtensionTimeWillExpire() { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } } extension UNNotificationAttachment { static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> 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 } }