SWIFT-将图像附加到推送通知

时间:2018-12-05 10:53:39

标签: swift push-notification apple-push-notifications push

我需要将URL图像附加到我从控制台php发送的推送通知中。对于标题和正文有任何问题,但我无法显示图像。但是我不想使用Notification Service类,因为我在该类中正在做所有事情。我该怎么做? 在AppDelegate的下面。感谢您的所有建议。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
 //OTHER CODE
struct Root : Codable {
        let title:String
        let message:String
        let image:String
    }
    let state = UIApplication.shared.applicationState
    if state == .background  || state == .inactive{
        if let str = userInfo["data"] as? String {
            let data = userInfo["data"] as? NSString
            print("DATA: \n\(data!)\n")
            let res = try? JSONDecoder().decode(Root.self,from:str.data(using:.utf8)!)
            let stringImage:String = res!.image
            if res!.image == "" {
                let center = UNUserNotificationCenter.current()
                let content = UNMutableNotificationContent()
                content.title = "\(res!.title)"
                content.body = "\(res!.message)"
                content.sound = .default
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                let request = UNNotificationRequest(identifier: "t", content: content, trigger: trigger)
                center.add(request, withCompletionHandler: nil)
            } else if res!.image == stringImage {
                let center = UNUserNotificationCenter.current()
                let content = UNMutableNotificationContent()
                let imageURL = URL(string: "\(res!.image)")! //this is my url that i send from console: http://demo-lab.it/app-php/okkey/push/upload/offerta_1.png
                print("URL IMAGE: \(imageURL)")
                content.title = "\(res!.title)"
                content.body = "\(res!.message)"
                //content.attachments = [attachment] //HELP ME.
                content.sound = .default
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                let request = UNNotificationRequest(identifier: "t", content: content, trigger: trigger)
                center.add(request, withCompletionHandler: nil)
            }
        }
    }else if state == .active {
        let str = userInfo["data"] as? String
        let res = try? JSONDecoder().decode(Root.self,from: str!.data(using:.utf8)!)
        self.showAlertAppDelegate(title: "\(res!.title)", message: "\(res!.message)", imageName: "\(res!.image)", buttonTitle: "OK", window: self.window!)
    }

0 个答案:

没有答案