溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS10新的通知機制中如何添加圖片

發(fā)布時間:2021-07-13 16:17:42 來源:億速云 閱讀:173 作者:小新 欄目:移動開發(fā)

小編給大家分享一下iOS10新的通知機制中如何添加圖片,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1、新建Target

iOS10新的通知機制中如何添加圖片

iOS10新的通知機制中如何添加圖片

2、實現(xiàn)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、發(fā)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"
}

這個很關(guān)鍵

最后運行,就會自動展示圖片啦。

需要說明的是,默認是只能https的,如果想顯示http圖片,需要自行設(shè)定ATS相關(guān)配置。

以上是“iOS10新的通知機制中如何添加圖片”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI