最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS 10新的通知機制中添加圖片的方法詳解

 更新時間:2017年03月25日 10:33:05   作者:四號程序員  
這篇文章主要介紹了iOS 10新的通知機制中添加圖片的方法,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

1、新建Target

2、實現(xiàn)UNNotificationServiceExtension

我這里用的是swift

//
// NotificationService.swift
// NotificationServiceExtension
//
// Created by Heyuan Li on 17/2/26.
// Copyright © 2017年 fenbi. All rights reserved.
//
 
import UserNotifications
 
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...
   bestAttemptContent.title = "上課啦"
 
   if let imageURLString = bestAttemptContent.userInfo["image"] as? String,
    let URL = URL(string: imageURLString)
   {
    downloadAndSave(url: URL) { localURL in
     if let localURL = localURL {
      do {
       let attachment = try UNNotificationAttachment(identifier: "image_downloaded", url: localURL, options: nil)
       bestAttemptContent.attachments = [attachment]
      } catch {
       print(error)
      }
     }
     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)
  }
 }
 
 private func downloadAndSave(url: URL, handler: @escaping (_ localURL: URL?) -> Void) {
  let task = URLSession.shared.dataTask(with: url, completionHandler: {
   data, res, error in
 
   var localURL: URL? = nil
 
   if let data = data {
    let ext = (url.absoluteString as NSString).pathExtension
    let cacheDirs = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
    if cacheDirs.count > 0 {
     let cacheDir = cacheDirs[cacheDirs.count - 1]
     /// TODO tutor cache
     /// TODO md5 extension
      let url = cacheDir.appendingPathComponent("123").appendingPathExtension(ext)
 
      if let _ = try? data.write(to: url) {
       localURL = url
      }
 
   }
   }
   
   handler(localURL)
  })
  
  task.resume()
 }
 
}

3、發(fā)Push的時候,加上”mutable-content”: 1

{
 "aps": {
 "alert": "instant message from CRM",
 "sound": "default", 
 "mutable-content": 1
 },
......
 "image": "https://g0.fbcontent.cn/api/tutor/images/153c6c68411747f.jpg"
}

這個很關鍵

最后運行,就會自動展示圖片啦。

需要說明的是,默認是只能https的,如果想顯示http圖片,需要自行設定ATS相關配置。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位iOS開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

最新評論

古交市| 凤庆县| 改则县| 乌审旗| 丰宁| 敦煌市| 建水县| 浦城县| 分宜县| 衡水市| 班玛县| 澎湖县| 镶黄旗| 改则县| 太康县| 儋州市| 海城市| 临泉县| 贵港市| 襄汾县| 西充县| 吴旗县| 凤冈县| 喜德县| 娄底市| 高青县| 伊宁市| 通州区| 比如县| 花莲市| 仙游县| 伊春市| 安泽县| 桂林市| 张家港市| 都兰县| 咸阳市| 长寿区| 垣曲县| 榆中县| 安宁市|