iOS 10新的通知機(jī)制中添加圖片的方法詳解
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的時(shí)候,加上”mutable-content”: 1
{ "aps": { "alert": "instant message from CRM", "sound": "default", "mutable-content": 1 }, ...... "image": "https://g0.fbcontent.cn/api/tutor/images/153c6c68411747f.jpg" }
這個(gè)很關(guān)鍵
最后運(yùn)行,就會(huì)自動(dòng)展示圖片啦。
需要說(shuō)明的是,默認(rèn)是只能https的,如果想顯示http圖片,需要自行設(shè)定ATS相關(guān)配置。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化
本篇文章主要介紹了使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能
這篇文章主要為大家詳細(xì)介紹了iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧2015-12-12