From 9c130b885dca9ad39c8f7be2adf5d7925f7cfdf4 Mon Sep 17 00:00:00 2001 From: Anantha Krishnan K G Date: Mon, 22 May 2017 14:45:26 +0530 Subject: [PATCH] Added guards in Rich push notification extension handler class (#51) * Updated the version to 3.2.3 Added guards in Rich push notification extension handler class * Added guard in Rich push notification extension handler class Added guard in Rich push notification extension handler class * Added guards in Rich push notification extension handler class Added guards in Rich push notification extension handler class --- BMSPush.podspec | 2 +- Source/BMSRichPushNotificationOptions.swift | 41 ++++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/BMSPush.podspec b/BMSPush.podspec index 120115b..dc500d7 100644 --- a/BMSPush.podspec +++ b/BMSPush.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = 'BMSPush' - s.version = '3.2.2' + s.version = '3.2.3' s.summary = 'Swift client side Push SDK for IBM Bluemix Push notifications services' s.homepage = 'https://github.com/ibm-bluemix-mobile-services/bms-clientsdk-swift-push' diff --git a/Source/BMSRichPushNotificationOptions.swift b/Source/BMSRichPushNotificationOptions.swift index 588e9d5..56a0463 100644 --- a/Source/BMSRichPushNotificationOptions.swift +++ b/Source/BMSRichPushNotificationOptions.swift @@ -36,25 +36,30 @@ open class BMSPushRichPushNotificationOptions:UNNotificationServiceExtension { var bestAttemptContent: UNMutableNotificationContent? bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) - let urlString = request.content.userInfo["attachment-url"] as? String - if 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: "video", url: tmpUrl, options:nil) { - bestAttemptContent?.attachments = [attachment] + if let urlString = request.content.userInfo["attachment-url"] as? String, urlString != "" { + + if 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: "video", url: tmpUrl, options:nil) { + bestAttemptContent?.attachments = [attachment] + } } - } - // Serve the notification content - contentHandler(bestAttemptContent!) - }.resume() + // Serve the notification content + contentHandler(bestAttemptContent!) + }.resume() + } + + }else{ + contentHandler(bestAttemptContent!) } } }