国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 余庆县| 行唐县| 安化县| 莱西市| 黎城县| 水富县| 宿迁市| 长乐市| 聂拉木县| 宁明县| 元氏县| 寿光市| 济源市| 尼玛县| 汶川县| 怀化市| 新巴尔虎右旗| 南木林县| 城口县| 昌邑市| 平乡县| 和顺县| 南江县| 涟水县| 孟连| 新津县| 靖远县| 海宁市| 贵州省| 容城县| 杂多县| 峨边| 霍邱县| 清新县| 贵南县| 泗阳县| 安乡县| 府谷县| 通海县| 汕头市| 聂拉木县|