iOS 10新的通知机制中添加图片的方法详解

时间:2021-05-02

1、新建target

2、实现unnotificationserviceextension

我这里用的是swift

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 // // 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

? 1 2 3 4 5 6 7 8 9 { "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开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章