欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 10新的通知機(jī)制中添加圖片的方法詳解

 更新時間:2017年03月25日 10:33:05   作者:四號程序員  
這篇文章主要介紹了iOS 10新的通知機(jī)制中添加圖片的方法,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

1、新建Target

2、實(shí)現(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)鍵

最后運(yùn)行,就會自動展示圖片啦。

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位iOS開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • 查看iOS Crash logs的方法

    查看iOS Crash logs的方法

    發(fā)布了一個應(yīng)用,用戶使用 的時候crash了,現(xiàn)在想調(diào)查為何crash,所以想在這里探討一下如何查看iphone 手機(jī)的crash logs
    2015-06-06
  • iOS之異常與信號使用場景分析

    iOS之異常與信號使用場景分析

    這篇文章主要為大家介紹了iOS之異常與信號使用場景分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS常見的幾個修飾詞深入講解

    iOS常見的幾個修飾詞深入講解

    這篇文章主要給大家介紹了關(guān)于iOS常見的幾個修飾詞的相關(guān)資料,iOS修飾詞包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • iOS開發(fā)中CALayer使用的基本教程

    iOS開發(fā)中CALayer使用的基本教程

    這篇文章主要介紹了iOS開發(fā)中CALayer使用的基本教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • IOS多線程編程的3種實(shí)現(xiàn)方法

    IOS多線程編程的3種實(shí)現(xiàn)方法

    這篇文章主要介紹了IOS多線程編程的3種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國際化

    詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國際化

    本篇文章主要介紹了使用jquery.i18n.properties 實(shí)現(xiàn)web前端國際化,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • iOS實(shí)現(xiàn)循環(huán)滾動公告欄

    iOS實(shí)現(xiàn)循環(huán)滾動公告欄

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)循環(huán)滾動公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能

    iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能

    這篇文章主要為大家詳細(xì)介紹了iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • iOS 11 BUG的發(fā)現(xiàn)、定位和解決

    iOS 11 BUG的發(fā)現(xiàn)、定位和解決

    這篇文章主要為大家詳細(xì)介紹了iOS 11 BUG的發(fā)現(xiàn)、定位和解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    近日不忙,給大家分享一個關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識,感興趣的朋友一起看看吧
    2015-12-12

最新評論