import UIKit
import UserNotifications
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//设置推送内容
let content = UNMutableNotificationContent()
content.title = "hangge.com"
content.body = "囤积iPhoneX的黄牛赔到怀疑人生?"
//给通知添加图片附件
if let imageURL = Bundle.main.url(forResource: "image", withExtension: "png"),
let attachment = try? UNNotificationAttachment(identifier: "imageAttachment",
url: imageURL, options: nil) {
content.attachments = [attachment]
}
//设置通知触发器
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
//设置请求标识符
let requestIdentifier = "com.hangge.testNotification"
//设置一个通知请求
let request = UNNotificationRequest(identifier: requestIdentifier,
content: content, trigger: trigger)
//将通知请求添加到发送中心
UNUserNotificationCenter.current().add(request) { error in
if error == nil {
print("Time Interval Notification scheduled: \(requestIdentifier)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1852.html
let content = notification.request.content
if let attachment = content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
eventImage.image = UIImage(contentsOfFile: attachment.url.path!)
attachment.url.stopAccessingSecurityScopedResource()
}
}