从URL(图片)发出加载unnotificationAttachment的问题

时间:2017-06-18 11:37:03

标签: ios swift uilocalnotification

我遇到通知附件的问题,当我向通知添加附件时,它将无法正常工作,但是当我删除附件的代码时,它可以解决问题而没有问题 任何人都可以尝试解决这个问题。

这是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

1 个答案:

答案 0 :(得分:1)

尝试以下代码

"\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85"

使用以下扩展名获取图像,该图像已添加到项目中。

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
    }
}