2016-09-08 37 views
2

我在iOS的10媒體在附件10的iOS推送通知

我添加了一個通知服務擴展添加圖片到我的推送通知掙扎,並用下面的代碼:

 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 
    self.contentHandler = contentHandler 
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) 


    if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) { 
     URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in 
      if let location = location { 
       let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG] 
       if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) { 
        self.bestAttemptContent?.attachments = [attachment] 
       } 
      } 
      self.contentHandler!(self.bestAttemptContent!) 
      }.resume() 
    } 
} 

我從下面的第一個答案中得到了這段代碼。

我現在遇到的問題是收到通知,並顯示一個短暫的延遲,表明下載必須發生,但沒有顯示附件。

我假設serviceExtensionTimeWillExpire()被調用,只是顯示bestAttempt

任何幫助是極大的讚賞。

我有我的APN有效載荷配置正確,我相信:

apns: { 
    aps: { 
    alert: { 
     title: "Title", 
     subtitle: "Subtitle", 
     body: "Body" 
    }, 
    "mutable-content": 1 
    }, 
    "image-url": "https://helloworld.com/image.png" 
} 

回答

2

你必須拉出來的網址通知的用戶信息的,然後下載圖像,並給出一個文件URL附件。嘗試是這樣的:

if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) { 
    URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in 
     if let location = location { 
      let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG] 
      if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) { 
       self.bestAttemptContent.attachments = [attachment] 
      } 
     } 

     self.contentHandler(self.bestAttemptContent) 
    }.resume() 
} 
+0

我似乎無法使用我的更新代碼的電影或GIF,只有圖像。有什麼建議麼? – nickjf89

0

我設法用下面的代碼工作了這一點:

斯威夫特:

if let PusherNotificationData = request.content.userInfo["data"] as? NSDictionary { 
      if let urlString = PusherNotificationData["image-url"] as? String, let fileUrl = URL(string: urlString) { 
       URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in 
        if let location = location { 
         let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG] 
         if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) { 
          self.bestAttemptContent?.attachments = [attachment] 
         } 
        } 

        self.contentHandler!(self.bestAttemptContent!) 
        }.resume() 
      } 
     } 

節點:

apns: { 
    aps: { 
     alert: { 
     title: "title", 
     subtitle: "subtitle", 
     body: "body" 
     }, 
     "mutable-content": 1, 
     category: "test" 
     }, 
    data: { 
     "image-url": "www.helloworld.com/image.png" 
    } 
    } 

感謝您的幫助!

+0

我似乎無法通過更改文件類型提示添加Gif或視頻。此外,刪除此選項會阻止加載我的圖像。它仍然應該從URL推斷文件類型,對吧? – nickjf89