国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2020-07-26 02:52:50
字體:
供稿:網(wǎng)友

1、新建Target

2、實(shí)現(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"}

這個很關(guān)鍵

最后運(yùn)行,就會自動展示圖片啦。

需要說明的是,默認(rèn)是只能https的,如果想顯示http圖片,需要自行設(shè)定ATS相關(guān)配置。

總結(jié)

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇远县| 米林县| 静安区| 佛冈县| 长丰县| 三河市| 云龙县| 丹巴县| 曲松县| 舞阳县| 平乡县| 十堰市| 三台县| 梅河口市| 若尔盖县| 昆山市| 马山县| 积石山| 门源| 西和县| 平邑县| 新田县| 彭州市| 屏东市| 兴化市| 博兴县| 北辰区| 疏附县| 宜川县| 西乌珠穆沁旗| 洱源县| 淳安县| 当雄县| 阿拉善盟| 来安县| 新余市| 迭部县| 石台县| 航空| 沙田区| 仁化县|