ios - Rich Notification in swift. Image not show in notification -
i make rich notification show image in notification whenever send simple message notification. last 2 day trying show image in notification not done. please me this.
thank in advance
this code.
in notification service extension
override func didreceive(_ request: unnotificationrequest, withcontenthandler contenthandler: @escaping (unnotificationcontent) -> void) { self.contenthandler = contenthandler bestattemptcontent = (request.content.mutablecopy() as? unmutablenotificationcontent) // custom data notification payload if let data = request.content.userinfo["data"] as? [string: string] { // grab attachment if let urlstring = data["attachment-url"], let fileurl = url(string: urlstring) { // download attachment urlsession.shared.downloadtask(with: fileurl) { (location, response, error) in if let location = location { // move temporary file remove .tmp extension let tmpdirectory = nstemporarydirectory() let tmpfile = "file://".appending(tmpdirectory).appending(fileurl.lastpathcomponent) let tmpurl = url(string: tmpfile)! try! filemanager.default.moveitem(at: location, to: tmpurl) // add attachment notification content if let attachment = try? unnotificationattachment(identifier: "", url: tmpurl) { self.bestattemptcontent?.attachments = [attachment] } } // serve notification content self.contenthandler!(self.bestattemptcontent!) }.resume() } } }
this notification structure
{ "aps" : { "alert" : { "title" : "push remote rich notifications", "subtitle" : "ios 10 - new api", "body" : "media image rich notification" }, "mutable-content" : 1, "category" : "imageidentifier" }, "data" : { "attachment-url": "https://raw.githubusercontent.com/sweefties/ios10-newapi-usernotifications-example/master/source/ios10-newapi-usernotifications-example.jpg" } }
Comments
Post a Comment