2016-11-22 11 views
1

ios 10で豊富なリモート通知を実装しようとしています。私はこのコードを実装しました。通知を受け取った後のコントロールはここにありますが、イメージをダウンロードして通知に表示する方法はわかりません。前もって感謝します。豊富なリモート通知のためのUNNotificationServiceExtensionの使用

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 { 
     // Modify the notification content here... 
     //print("title for image = \(bestAttemptContent.title)") 
     bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" 

     contentHandler(bestAttemptContent) 
    } 

} 



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

あなたnotificationData

このような
"attachment-url": "https://yourimage.png" 

で添付ファイルを取得し、これはあなたがそれをhere

+0

お返事ありがとう@Rajat。私はこれを試みましたが、まだ画像をダウンロードしていません。テキストデータだけを表示します。私が間違っていることが分かりません – Prajyot

+0

ファイルがダウンロードされているかどうかをチェックし、通知データから有効なURLを取得していることを確認してください。 – Rajat

+0

はい、URLが有効です。しかし、どこの場所に保管されるのですか?私は写真を撮ったが、何もなかった。 – Prajyot

0

最後にその作業から参照

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

     // Get the custom data from the notification payload 
     if let notificationData = request.content.userInfo["data"] as? [String: String] { 
      // Grab the attachment 
      if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) { 
       // Download the attachment 
       URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in 
        if let location = location { 
         // Move temporary file to remove .tmp extension 
         let tmpDirectory = NSTemporaryDirectory() 
         let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent) 
         let tmpUrl = URL(string: tmpFile)! 
         try! FileManager.default.moveItem(at: location, to: tmpUrl) 

         // Add the attachment to the notification content 
         if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) { 
          self.bestAttemptContent?.attachments = [attachment] 
         } 
        } 
        // Serve the notification content 
        self.contentHandler!(self.bestAttemptContent!) 
        }.resume() 
      } 
     } 

を使用する方法です。問題は、ここで私は

NSAppTransportSecurity延長のplistの中

タグを追加しなければならなかったことでした。このタグを追加すると、画像が表示され始めました。誰かを助けることを願っています。

関連する問題