Created
August 16, 2016 12:50
-
-
Save janodev/0177c5140a54c8d1e5edef52ccd78a83 to your computer and use it in GitHub Desktop.
Create a notification attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UserNotifications | |
class ServiceExtension: UNNotificationServiceExtension | |
{ | |
struct Attachments { | |
static let image = (identifier:"image", jsonElement:"imageURL") | |
} | |
var contentHandler: ((UNNotificationContent) -> Void)? | |
var content: UNMutableNotificationContent? | |
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler:(UNNotificationContent) -> Void) | |
{ | |
self.contentHandler = contentHandler | |
content = (request.content.mutableCopy() as? UNMutableNotificationContent) | |
if let content = content { | |
if let urlString = content.userInfo[Attachments.image.jsonElement] as? String, let attachmentURL = URL(string: urlString) { | |
if let attachment = try? UNNotificationAttachment(identifier: Attachments.image.identifier, url: attachmentURL, options: nil) { | |
content.attachments = [attachment] | |
} | |
} | |
return contentHandler(content) | |
} | |
contentHandler(request.content) | |
} | |
override func serviceExtensionTimeWillExpire() | |
{ | |
if let contentHandler = contentHandler, let content = content { | |
contentHandler(content) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment