-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Sources/AppcuesNotificationService/AppcuesNotificationServiceExtension.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// AppcuesNotificationServiceExtension.swift | ||
// AppcuesNotificationService | ||
// | ||
// Created by Matt on 2024-03-12. | ||
// Copyright © 2024 Appcues. All rights reserved. | ||
// | ||
|
||
import UserNotifications | ||
|
||
/// `UNNotificationServiceExtension` subclass that implements Appcues functionality. | ||
/// | ||
/// ## Basic Usage | ||
/// ```swift | ||
/// // In your Notification Service Extension | ||
/// import AppcuesNotificationService | ||
/// class NotificationService: AppcuesNotificationServiceExtension {} | ||
/// ``` | ||
open class AppcuesNotificationServiceExtension: UNNotificationServiceExtension { | ||
|
||
var contentHandler: ((UNNotificationContent) -> Void)? | ||
var bestAttemptContent: UNMutableNotificationContent? | ||
|
||
override public func didReceive( | ||
_ request: UNNotificationRequest, | ||
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void | ||
) { | ||
self.contentHandler = contentHandler | ||
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | ||
|
||
if let attachment = request.attachment { | ||
bestAttemptContent?.attachments = [attachment] | ||
} | ||
|
||
contentHandler(bestAttemptContent ?? request.content) | ||
} | ||
|
||
override public 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) | ||
} | ||
} | ||
} | ||
|
||
extension UNNotificationRequest { | ||
var attachment: UNNotificationAttachment? { | ||
guard let attachment = content.userInfo["appcues_attachment_url"] as? String, | ||
let attachmentType = content.userInfo["appcues_attachment_type"] as? String, | ||
let attachmentURL = URL(string: attachment), | ||
let imageData = try? Data(contentsOf: attachmentURL) else { | ||
return nil | ||
} | ||
return try? UNNotificationAttachment(data: imageData, dataType: attachmentType, options: nil) | ||
} | ||
} | ||
|
||
extension UNNotificationAttachment { | ||
convenience init(data: Data, dataType: String, options: [NSObject: AnyObject]?) throws { | ||
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString | ||
let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true) | ||
|
||
try FileManager.default.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil) | ||
|
||
let imageFileIdentifier = UUID().uuidString + "." + dataType | ||
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier) | ||
|
||
try data.write(to: fileURL) | ||
|
||
try self.init(identifier: imageFileIdentifier, url: fileURL, options: options) | ||
} | ||
} |
This file contains 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