服务器之家

服务器之家 > 正文

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

时间:2021-03-11 15:46     来源/作者:四号程序员

1、新建target

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

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

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开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部