2017-06-12 3 views
0

私は昨年iOS 10でAppleによって発表された機能を使用しています。通知の画像が空であることがあるという問題があります。UNUserNotificationCenterを使用してリモート通知からの空のコンテンツイメージ

これは私のUNNotificationServiceExtensionです。私が間違っていることは本当にわかりません。画像のサイズは最大1 MBです。サーバーからのペイロードが正しくあります。

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) 

     if let bestAttemptContent = bestAttemptContent { 
      // Setting the category associated with the notification 
      if let category = bestAttemptContent.userInfo["category"] as? String { 
       bestAttemptContent.categoryIdentifier = category 
      } 

      // Fetching luubra if available 
      if let attachmentString = bestAttemptContent.userInfo["image"] as? String, 
       let attachmentUrl = URL(string: attachmentString) { 

       let session = URLSession(configuration: URLSessionConfiguration.default) 
       let attachmentDownloadTask = session.downloadTask(with: attachmentUrl, 
                    completionHandler: { url, _, error in 
         if let error = error { 
          print("Error downloading notification image: \(error)") 
         } else if let url = url { 
          do { 
           let attachment = try UNNotificationAttachment(identifier: attachmentString, 
                       url: url, 
                       options: [UNNotificationAttachmentOptionsTypeHintKey: kUTTypeJPEG]) 
           bestAttemptContent.attachments = [attachment] 
          } catch let e { 
           print("Error creating NotificationAttachment: \(e)") 
          } 
         } 
         print("Remote notification content: \(bestAttemptContent)") 
         contentHandler(bestAttemptContent) 
       }) 
       attachmentDownloadTask.resume() 

      } 
     } 
    } 

    override func serviceExtensionTimeWillExpire() { 
     // Called just before the extension will be terminated by the system. 
     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. 
     if let contentHandler = contentHandler, 
      let bestAttemptContent = bestAttemptContent { 
      contentHandler(bestAttemptContent) 
     } 
    } 

} 

答えて

0

Appleは、タイトルまたは本文のようにカテゴリを直接設定しているようです。 メディアを一時的にディスクに保存することが重要です。 iOSではメディアファイルをダウンロードするのに時間がかかるためです。 iOSが残りの部分を処理します。

この作品は私にとって素晴らしいことを知っています。

class NotificationService: UNNotificationServiceExtension { 

    var contentHandler: ((UNNotificationContent) -> Void)? 
    var bestAttemptContent: UNMutableNotificationContent? 

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 

     self.contentHandler = contentHandler 
     self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) 

     func failed() { 
      contentHandler(request.content) 
     } 

     guard let contentHandler = self.contentHandler, let bestAttemptContent = self.bestAttemptContent else { 
      return failed() 
     } 

     // Get the image from the User Payload 
     guard let imageURLString = request.content.userInfo["image"] as? String else { 
      return failed() 
     } 

     guard let imageURL = URL(string: imageURLString) else { 
      return failed() 
     } 

     // Download the Image Async 
     URLSession.shared.downloadTask(with: imageURL) { (path, _, error) in 

      if let error = error { 
       print(error.localizedDescription) 
      } 

      if let path = path { 

       // Save the image temporary to the disk 
       let tmpDirectory = NSTemporaryDirectory() 
       let tmpFile = "file://".appending(tmpDirectory).appending(imageURL.lastPathComponent) 
       guard let tmpURL = URL(string: tmpFile) else { return } 
       try? FileManager.default.moveItem(at: path, to: tmpURL) 

       // Add the attachment to the notification content 
       if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpURL) { 
        bestAttemptContent.attachments = [attachment] 
       } 
      } 

      // Serve the notification content 
      contentHandler(bestAttemptContent) 

     }.resume() 

    } 

    override func serviceExtensionTimeWillExpire() { 
     // Called just before the extension will be terminated by the system. 
     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. 
     if let contentHandler = contentHandler, 
      let bestAttemptContent = bestAttemptContent { 
      contentHandler(bestAttemptContent) 
     } 
    } 
} 
関連する問題