我对通知附件有问题,当我将附件添加到通知时它不会工作,但是当我删除附件代码时它工作 probley 没有问题 任何人都可以尝试解决这个问题。
这是 AppDelegate 中的通知函数:
@available(iOS 10.0, *)
func scheduleNotification(currentGame: games) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let calendar = Calendar(identifier: .gregorian)
let comp = calendar.dateComponents(in:.current, from: dateFormatter.date(from: currentGame.gameDate)!)
let newComponents = DateComponents(calendar: calendar ,year: comp.year ,month: comp.month , day: comp.day, hour: comp.hour, minute: comp.minute ,second: comp.second)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = NSLocalizedString("NOTIFICATION_TITLE", comment: "Game Released")
content.body = "\(currentGame.gameName) Released ✌🏻"
content.sound = UNNotificationSound.default()
content.badge = 1
if let url = NSURL(string: "http://www.ya-techno.com/gamesImage/\(currentGame.gameImage)"){
do {
let attachment = try UNNotificationAttachment(identifier: "image", url: url as URL, options: nil)
content.attachments = [attachment]
} catch {
print("The attachment was not loaded.")
}
}
let request = UNNotificationRequest(identifier:currentGame.gameID, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("error: \(error)")
}
}
}
@available(iOS 10.0, *)
func removeAllPendingNotificationRequests(){
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
最佳答案
试试下面的代码
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let identifier = ProcessInfo.processInfo.globallyUniqueString
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "World"
content.sound = UNNotificationSound.default()
let myImage: UIImage = UIImage(named: "logo.png")!
if let attachment = UNNotificationAttachment.create(identifier: identifier, image: myImage, options: nil) {
// where myImage is any UIImage that follows the
content.attachments = [attachment]
}
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
// handle error
}
}
使用下面的扩展来获取您在项目中添加的图像。
extension UNNotificationAttachment {
/// Save the image to disk
static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
let imageFileIdentifier = identifier+".png"
let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
guard let imageData = UIImagePNGRepresentation(image) else {
return nil
}
try imageData.write(to: fileURL)
let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
return imageAttachment
} catch {
print("error " + error.localizedDescription)
}
return nil
}
}
关于ios - 从 URL(图片)发出加载 unnotificationAttachment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44614562/