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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:47:35
字體:
來源:轉載
供稿:網友

1、新建Target

ios,通知機制,ios的通知機制,添加通知

ios,通知機制,ios的通知機制,添加通知

2、實現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、發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相關配置。

總結

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


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平遥县| 邮箱| 泌阳县| 固阳县| 许昌县| 界首市| 定襄县| 大冶市| 临泉县| 会昌县| 西峡县| 西宁市| 石柱| 拜城县| 柯坪县| 武功县| 西畴县| 泸溪县| 白山市| 乌什县| 牡丹江市| 仙桃市| 云林县| 中牟县| 文安县| 新蔡县| 安乡县| 礼泉县| 开远市| 广汉市| 遵义市| 天全县| 永胜县| 龙山县| 义乌市| 佛学| 天门市| 慈利县| 昭平县| 富川| 疏勒县|