热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

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

这篇文章主要介绍了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相关配置。

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。


推荐阅读
author-avatar
黑痣佬
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有