ios - 从 URL(图片)发出加载 unnotificationAttachment

标签 ios swift uilocalnotification

我对通知附件有问题,当我将附件添加到通知时它不会工作,但是当我删除附件代码时它工作 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()

  }

这是来自控制台的图像: console error

最佳答案

试试下面的代码

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/

相关文章:

ios - 为什么我的 UILocalNotification 不播放任何声音?

iphone - 计划的本地通知未存储在 scheduledLocalNotification 数组中

ios - 如何在 React Native 中使用 UITableViewCellStyleSubtitle 获取原生 UITableView?

ios - 在不清除空间的情况下隐藏 iOS 7 中的状态栏?

ios - geocodeaddressString 提供了错误的坐标

swift - 在cocoa中,NSTextField没有响应

ios - 日期之间的提醒问题

IOS7 NSURLConnection 没有任何反应

ios - 提交给 Apple : XCode "submit" or Application Loader?

Ios Firebase fatal error