小编给大家分享一下iOS10新的通知机制中如何添加图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1、新建Target
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相关配置。
以上是“iOS10新的通知机制中如何添加图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:iOS10新的通知机制中如何添加图片-创新互联
网站网址:https://www.cdcxhl.com/article34/djddse.html
成都网站建设公司_创新互联,为您提供网站改版、用户体验、面包屑导航、全网营销推广、品牌网站设计、营销型网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联