时间: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邮箱联系删除。
IOS中Weex加载.xcassets中的图片资源的实例详解前言:因为.xcassets中的图片资源只能通过imageNamed:方法加载,所以需要做一些特殊处
推荐阅读:iOS10推送之基础知识(必看篇)这篇文章开始,我会跟大家好好讲讲,苹果新发布的iOS10的所有通知类。一、创建本地通知事例详解:注意啊,小伙伴们,本
iOS10.2.1正式版推出后,苹果又在今天凌晨推送了iOS10.3的首个测试版,而这也会是iOS10的又一个重磅更新。iOS10.3新的测试版中添加了不少新功
虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式。iOS10有了新的通知,如新消息、商业信息和时间表的变化。在本教程中,
IOS改变导航栏返回按钮的标题实例详解前言:下午又找到了一个新的方法这个方法不错暂时没有发现异常的地方。新写的App中需要使用UINavigationContr