欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

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

时间:2023-01-17 12:46:25|栏目:iOS代码|点击:

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应用开发中图片的拉伸问题解决方案

栏    目:iOS代码

下一篇:没有了

本文标题:iOS 10新的通知机制中添加图片的方法详解

本文地址:http://www.codeinn.net/misctech/223813.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有