//
// 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()
}
}
{
"aps": {
"alert": "instant message from CRM",
"sound": "default",
"mutable-content": 1
},
......
"image": "https://g0.fbcontent.cn/api/tutor/images/153c6c68411747f.jpg"
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有