2016-11-22 2 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

"attachment-url": "https://yourimage.png" 

처럼 notificationData의 첨부 파일을 얻을 것이다 그리고 이것은 당신이 그것을

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() 
      } 
     } 

마지막 here

+0

@Rajat 답장을 보내 주셔서 감사합니다. 이것을 시도했지만 여전히 이미지를 다운로드하지 않습니다. 텍스트 데이터 만 계속 표시합니다. 내가 잘못하고있는 것이 무엇인지 모르겠다. – Prajyot

+0

파일 다운로드 여부를 확인하고 알림 데이터에서 올바른 URL을 확인하십시오. – Rajat

+0

예 url이 유효합니다. 그러나 어떤 위치에 그들은 저장 될 예정입니까 ?? 나는 사진을 통해 갔다. – Prajyot

0

의 작업에서 참조 사용할 수있는 방법입니다. 문제는 여기에 내가

NSAppTransportSecurity 확장의 PLIST에

태그를 추가했다이었다. 이 태그를 추가 한 후 이미지를 표시하기 시작했습니다. 누군가가 도움이되기를 바랍니다.

관련 문제