iOS Rich notifications not working correctly - ios

I need to implement rich notification in current application with image.
I have implemented everything and its working fine. And receiving push notification, but getting only title and message but not image as attachments
Normal notifications without mutable-content work fine.
As far as I tested, there is no problem with certificate side.. I am using NotificationService extension to show media attachment
Any idea?
My payload looks like below:
[AnyHashable("title"): New trailer released- Hindi Movies, AnyHashable("message"): Kuchh Bheege Alfaaz - Official Hindi Trailer, AnyHashable("aps"): {
alert = "New trailer released- Hindi Movies";
badge = 2;
sound = default;
}, AnyHashable("trailer_img"): uploads/trailer_image/9703831856a7968b8b5cf6b10357dc2c1.jpg]
My NotificationService Class code like below:
class NotificationService: UNNotificationServiceExtension {
var contentHandler : ((UNNotificationContent) -> Void)?
var content : UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler:
#escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.content = (request.content.mutableCopy()
as? UNMutableNotificationContent)
let userInfo : [AnyHashable: Any] = request.content.userInfo
print(userInfo)
if let bca = self.content {
func save(_ identifier: String,
data: Data, options: [AnyHashable: Any]?)
-> UNNotificationAttachment? {
let directory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString,
isDirectory: true)
do {
try FileManager.default.createDirectory(at: directory,
withIntermediateDirectories: true,
attributes: nil)
let fileURL = directory.appendingPathComponent(identifier)
try data.write(to: fileURL, options: [])
return try UNNotificationAttachment.init(identifier: identifier,
url: fileURL,
options: options)
} catch {
}
return nil
}
func exitGracefully(_ reason: String = "") {
let bca = request.content.mutableCopy()
as? UNMutableNotificationContent
bca!.title = reason
contentHandler(bca!)
}
let reachability = Reachability()!
reachability.whenReachable = { reachability in
DispatchQueue.main.async {
if reachability.isReachableViaWiFi {
guard let content = (request.content.mutableCopy()
as? UNMutableNotificationContent) else {
return exitGracefully()
}
let userInfo : [AnyHashable: Any] = request.content.userInfo
print(userInfo)
guard let attachmentURL = userInfo["trailer_img"]
as? String else {
return exitGracefully()
}
guard let imageData =
try? Data(contentsOf: URL(string: "http://techindiana.com/dev/truetrailer_app/" + attachmentURL)!)
else {
return exitGracefully()
}
guard let attachment =
save("image.png", data: imageData, options: nil)
else {
return exitGracefully()
}
content.attachments = [attachment]
contentHandler(content.copy() as! UNNotificationContent)
} else {
return exitGracefully()
}
}
}
reachability.whenUnreachable = { reachability in
DispatchQueue.main.async {
return exitGracefully()
}
}
do {
try reachability.startNotifier()
} catch {
return exitGracefully()
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bca = self.content {
contentHandler(bca)
}
}
}

Related

I'm making a rich push notifications in my iOS app but the notifications appeared without image why?

import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
var urlString:String? = nil
if let urlImageString = request.content.userInfo["urlImageString"] as? String {
urlString = urlImageString
}
if urlString != nil, let fileUrl = URL(string: urlString!) {
print("fileUrl: \(fileUrl)")
guard let imageData = NSData(contentsOf: fileUrl) else {
contentHandler(bestAttemptContent)
return
}
guard let attachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "image.jpg", data: imageData, options: nil) else {
print("error in UNNotificationAttachment.saveImageToDisk()")
contentHandler(bestAttemptContent)
return
}
bestAttemptContent.attachments = [ attachment ]
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
#available(iOSApplicationExtension 10.0, *)
extension UNNotificationAttachment {
static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)
do {
try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL!, options: [])
let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
return attachment
} catch let error {
print("error \(error)")
}
return nil
}
}
I tried using more than one source, but the result is the same, the notification appears without a picture. What is the reason here? How can I solve the problem?

Images in iOS push notification

I am trying to send images in push notifications
I have made the notifications registrations in app delegate and apns device token is generating properly.
ALso I have coded in service ext as follows:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let notificationData = request.content.userInfo["data"] as? [String: String] {
// Grab the attachment
if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
// Move temporary file to 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 the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
self.bestAttemptContent?.attachments = [attachment]
}
}
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
}
}
.
And the payload in json is as follows
{
"aps":
{"sound":"default","alert":
{"title":"iOS","body":"Hello Dude...."},
"mutable-content": 1},
"CustomData":
{"mType":"alert","m":"Hello Dude...."},
"Attachement-url":"https://pusher.com/static_logos/320x320.png"
}
I am receiving the title and message but image is not coming.
Please guide how to get image in push notifications
For Swift, If you want you can try with this framework
Also Add "content-available":1 in your aps
OR you can try downloading like this,
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as?UNMutableNotificationContent)
bestAttemptContent?.title = request.content.title
bestAttemptContent?.body = request.content.body
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()
}
guard let payload = content.userInfo["CustomData"] as? [String: Any] else {
return failEarly()
}
guard let attachmentURL = payload["Attachement-url"] as? String else {
return failEarly()
}
let identifierName = getIdentifierName(fileURL: attachmentURL)
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
guard let imageData = NSData(contentsOf:NSURL(string: attachmentURL)! as URL) else { return failEarly() }
guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: identifierName, data: imageData, options: nil, tmpSubFolderName: tmpSubFolderName) else { return failEarly() }
content.attachments = [attachment]
contentHandler(content.copy() as! UNNotificationContent)
}
}
func getIdentifierName(fileURL : String) -> String {
var identifierName : String = "image.jpg"
if !fileURL.isEmpty() {
identifierName = "file.\((fileURL as NSString).lastPathComponent)"
}
return identifierName
}
func failEarly() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
extension UNNotificationAttachment {
static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?, tmpSubFolderName : String) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let fileURLPath = NSURL(fileURLWithPath: NSTemporaryDirectory())
let tmpSubFolderURL = fileURLPath.appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL!, options: [])
let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL!, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}
This line:
if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
Is looking for an attachment-url value as a child of a data object in the userInfo dictionary.
It is looking for this:
{
"aps" : {
...
},
"data" : {
"attachment-url" : "some url"
}
}
But the payload in your question is this:
{
"aps":{
"sound":"default",
"alert": {
"title":"iOS",
"body":"Hello Dude...."
},
"mutable-content": 1
},
"CustomData": {
"mType":"alert",
"m":"Hello Dude...."
},
"Attachement-url":"https://pusher.com/static_logos/320x320.png"
}
The "data" section does not exist, and the attachment-url key does not exist.
Change your Swift code to match what is in the payload and you should be able to get the image URL and download it.
You will have a big problem if you receive a notification that does not have the attachment URL key or the attachment URL is not a properly formed URL. In those cases your if let will not be entered and the contentHandler will not be called! This will not just cause the service extension to lock up, but it will prevent any notification that does not have the attachment URL from being delivered! Add an else that calls the contentHandler to fix this.
Once you have it downloaded though there is another problem. iOS will need to know what kind of data you are putting in the attachment. The attachment options dictionary allows you to include type information about the attachment. Get the MIME Type of the downloaded file and create a Uniform Type Identifier from that. The Uniform Type Identifier string can then be used in the options dictionary.
I cover all of this in depth in the iOS Notifications book. The sample chapter available now deals with adding images to notifications.

iOS Rich Push notifications with Xcode, Swift3 but cannot get image

I am trying to create iOS Rich Push notifications with Xcode, Swift3.
I already make sure about push notifications (subject, body) with curl command of php but I can't create Rich Push Notifications referred to in this document.
I added Notification Service Extension like this: 「 File 」→「 New 」→「 Target... 」→「 Notification Service Extension 」 and also I added in 「'mutable_content': True」 curl command.
Then run but not call 「class NotificationService: UNNotificationServiceExtension」 so can't view push notifications image.
The following my code
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
let imageKey = AnyHashable("gcm.notification.image_url")
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let imageUrl = request.content.userInfo[imageKey] as? String {
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: URL(string: imageUrl)!, completionHandler: { [weak self] (data, response, error) in
if let data = data {
do {
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("push.png")
try data.write(to: writePath)
guard let wself = self else {
return
}
if let bestAttemptContent = wself.bestAttemptContent {
let attachment = try UNNotificationAttachment(identifier: "nnsnodnb_demo", url: writePath, options: nil)
bestAttemptContent.attachments = [attachment]
contentHandler(bestAttemptContent)
}
} catch let error as NSError {
print(error.localizedDescription)
guard let wself = self else {
return
}
if let bestAttemptContent = wself.bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
} else if let error = error {
print(error.localizedDescription)
}
})
task.resume()
} else {
if let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
REF : https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications
I have done like this hope it helps for GIF images you can change extension to .png.
Make sure that in APNS payload attachment-url is coming for image.
Check for App transport security key in case of image url start from http://...
Your image should be under ~200px. For me it not works beyond(HIT and TRIAL).
Code:
import UserNotifications
import SDWebImage
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
func failEarly() {
contentHandler(request.content)
}
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()
}
guard let attachmentURL = content.userInfo["attachment-url"] as? String else {
return failEarly()
}
SDWebImageDownloader.shared().downloadImage(with: URL(string: attachmentURL)!,
options: SDWebImageDownloaderOptions.continueInBackground,
progress: nil) { (image, data, error, flag) in
guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image.gif",
data: data! as NSData,
options: nil) else { return failEarly() }
content.attachments = [attachment]
contentHandler(content.copy() as! UNNotificationContent)
if let bestAttemptContent = self.bestAttemptContent {
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
contentHandler(bestAttemptContent)
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationAttachment {
static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL!, options: [])
let imageAttachment = try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL!, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}

Rich Notification in swift. Image not show in notification

I make rich notification to show image in notification but whenever i send simple message then i get notification. Last 2 day i am trying to show image in notification but it was not done. Please help me to do this.
Thank you in advance
This is my code.
In Notification Service Extension
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let data = request.content.userInfo["data"] as? [String: String] {
// Grab the attachment
if let urlString = data["attachment-url"], let fileUrl = URL(string: urlString) {
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
// Move temporary file to 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 the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
self.bestAttemptContent?.attachments = [attachment]
}
}
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
}
This is my 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"
}
}
Use this following code
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = bestAttemptContent else {
return
}
guard let attachmentUrlString = request.content.userInfo["pic_url"] as? String else {
return
}
guard let url = URL(string: attachmentUrlString) else {
return
}
URLSession.shared.downloadTask(with: url, completionHandler: { (optLocation: URL?, optResponse: URLResponse?, error: Error?) -> Void in
if error != nil {
print("Download file error: \(String(describing: error))")
return
}
guard let location = optLocation else {
return
}
guard let response = optResponse else {
return
}
do {
let lastPathComponent = response.url?.lastPathComponent ?? ""
var attachmentID = UUID.init().uuidString + lastPathComponent
if response.suggestedFilename != nil {
attachmentID = UUID.init().uuidString + response.suggestedFilename!
}
let tempDict = NSTemporaryDirectory()
let tempFilePath = tempDict + attachmentID
try FileManager.default.moveItem(atPath: location.path, toPath: tempFilePath)
let attachment = try UNNotificationAttachment.init(identifier: attachmentID, url: URL.init(fileURLWithPath: tempFilePath))
bestAttemptContent.attachments.append(attachment)
}
catch {
print("Download file error: \(String(describing: error))")
}
OperationQueue.main.addOperation({() -> Void in
self.contentHandler?(bestAttemptContent);
})
}).resume()
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
also the following code in info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
add this key in NSExtension NSDictionary in Info.plist
<key>NSExtensionAttributes</key>
<dict/>

How to show image in UNNotificationServiceExtension?

I added the UNNotificationServiceExtension to the application, I successfully get the data, and I load the image that comes in the data to push notification, but I can not display this image in the push notification window. Please tell me how it can be fixed?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "Apple [modified]"
bestAttemptContent.body = "Xcode"
contentHandler(bestAttemptContent)
let attachmentStorage = AttachmentStorage()
if let imagePath = request.content.userInfo["image"] as? String {
guard let url = URL(string: imagePath) else {
contentHandler(bestAttemptContent)
return
}
debugPrint("url", url)
attachmentStorage.store(url: url) { (localURL, error) in
if let localURL = localURL {
debugPrint("path", localURL)
do {
let attachment = try UNNotificationAttachment.init(identifier: "image", url: localURL, options: nil)
bestAttemptContent.attachments = [attachment]
debugPrint("bestAttemptContent.attachments", bestAttemptContent.attachments, bestAttemptContent.attachments.count)
contentHandler(bestAttemptContent)
return
} catch {
contentHandler(bestAttemptContent)
return
}
}
}
}
contentHandler(bestAttemptContent)
}
}
class AttachmentStorage {
func store(url: URL, completion: ((URL?, Error?) -> ())?) {
// obtain path to temporary file
let filename = ProcessInfo.processInfo.globallyUniqueString
let path = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(filename).jpeg")
// fetch attachment
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
let _ = try data?.write(to: path)
completion?(path, error)
} catch {
debugPrint(error.localizedDescription)
completion?(nil, error)
}
}
task.resume()
}
}
Logs
"bestAttemptContent.attachments" [<UNNotificationAttachment: 0x14dd626c0; identifier: image, family: Image, URL: file:///private/var/mobile/Containers/Data/PluginKitPlugin/1F13F774-B6CA-4567-9EF0-EA61C9A1F2A0/tmp/23A8E7EC-29FB-4C36-893D-5B0DCFB711B2-13808-000002A699FBF67B.jpeg, type: public.jpeg, options: <UNImageNotificationAttachmentOptions: 0x14dd5afd0>>] 1
I added this code. And even if you remove contentHandler sometimes you get duplicates or there were no images
let request = UNNotificationRequest.init(identifier: "image", content: bestAttemptContent, trigger: nil)
UNUserNotificationCenter.current().add(request) { (error) in
// handle error
}
I made this and it works for me
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
if let imagePath = request.content.userInfo["image"] as? String {
guard let url = URL(string: imagePath) else {
contentHandler(bestAttemptContent)
return
}
guard let imageData = NSData(contentsOf: url) else {
contentHandler(bestAttemptContent)
return
}
guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image.jpg", data: imageData, options: nil) else {
print("error in UNNotificationAttachment.create()")
contentHandler(bestAttemptContent)
return
}
bestAttemptContent.attachments = [ attachment ]
}
contentHandler(bestAttemptContent)
}
}
extension UNNotificationAttachment {
/// Save the image to disk
static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
guard let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true) else { return nil }
do {
try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL, options: [])
let imageAttachment = try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}
here a working code. Also verify the certificates are valid. I recommend you the RayWenderlich Screencasts of ios10 Notifications.
Try to delete the contentHandler after you change the title and body and the lastone
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
var attachmentString = ""
if let bigImage = bestAttemptContent.userInfo["gcm.notification.bigImage"] as? String {
attachmentString = bigImage
}
if attachmentString != "", let attachmentUrl = URL(string: attachmentString)
{
let session = URLSession(configuration: URLSessionConfiguration.default)
let attachmentDownloadTask = session.downloadTask(with: attachmentUrl, completionHandler: { (url, response, error) in
if let error = error {
print("Error downloading attachment: \(error.localizedDescription)")
} else if let url = url {
let attachment = try! UNNotificationAttachment(identifier: attachmentString, url: url, options: [UNNotificationAttachmentOptionsTypeHintKey : kUTTypePNG])
bestAttemptContent.attachments = [attachment]
}
contentHandler(bestAttemptContent)
})
attachmentDownloadTask.resume()
}
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}

Resources