Skip to content

Commit 97808ce

Browse files
authored
feat: add support for sound and video notification attachments (#10)
1 parent 3c0738f commit 97808ce

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 5.6
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

Sources/AmplifyUtilsNotifications/AUNotificationPayload.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,33 @@
77

88
import Foundation
99

10-
/// Defines the location of the image URL in a notification payload.
10+
/// Defines the location of the media URL in a notification payload.
1111
///
1212
/// Conform to this protocol to define notification payload formats for use with AUNotificationService.
1313
/// It is not necessary to define the full message schema. Defining the subset of the message that
14-
/// contains the remote image URL is sufficient. See `PinpointNotificationPayload` for an example.
14+
/// contains the remote media URL is sufficient. See `PinpointNotificationPayload` for an example.
15+
///
16+
/// Supported Media Types:
17+
///
18+
/// Audio - 5MB max
19+
/// - kUTTypeAudioInterchangeFileFormat
20+
/// - kUTTypeWaveformAudio
21+
/// - kUTTypeMP3
22+
/// - kUTTypeMPEG4Audio
23+
///
24+
/// Image - 10 MB max
25+
/// - kUTTypeJPEG
26+
/// - kUTTypeGIF
27+
/// - kUTTypePNG
28+
///
29+
/// Movie - 50 MB max
30+
/// - kUTTypeMPEG
31+
/// - kUTTypeMPEG2Video
32+
/// - kUTTypeMPEG4
33+
/// - kUTTypeAVIMovie
34+
///
1535
public protocol AUNotificationPayload: Decodable {
16-
var remoteImageURL: String? { get }
36+
var remoteMediaURL: String? { get }
1737
}
1838

1939
extension AUNotificationPayload {

Sources/AmplifyUtilsNotifications/AUNotificationService.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ open class AUNotificationService: UNNotificationServiceExtension {
4949

5050
if let bestAttemptContent = bestAttemptContent {
5151
// Modify the notification content
52-
guard let attachment = getImageAttachment(request) else { return }
52+
guard let mediaAttachment = getMediaAttachment(request) else { return }
5353

5454
os_log(.debug, "created attachment")
55-
bestAttemptContent.attachments = [attachment]
55+
bestAttemptContent.attachments = [mediaAttachment]
5656
}
5757
}
5858

@@ -67,15 +67,15 @@ open class AUNotificationService: UNNotificationServiceExtension {
6767
}
6868
}
6969

70-
private func getImageAttachment(_ request: UNNotificationRequest) -> UNNotificationAttachment? {
70+
private func getMediaAttachment(_ request: UNNotificationRequest) -> UNNotificationAttachment? {
7171
guard let payloadData = try? payloadSchema.init(decoding: request.content.userInfo),
72-
let mediaURLString = payloadData.remoteImageURL,
72+
let mediaURLString = payloadData.remoteMediaURL,
7373
let mediaType = mediaURLString.split(separator: ".").last,
7474
let mediaURL = URL(string: mediaURLString),
7575
let mediaData = try? self.loadDataFromURL(mediaURL) else {
7676
return nil
7777
}
78-
os_log(.debug, "got image data")
78+
os_log(.debug, "got media data")
7979

8080
let fileManager = FileManager.default
8181
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
@@ -87,12 +87,11 @@ open class AUNotificationService: UNNotificationServiceExtension {
8787
withIntermediateDirectories: true,
8888
attributes: nil)
8989

90-
// supported image types: jpg, gif, png
91-
let imageFileIdentifier = "\(UUID().uuidString).\(String(mediaType))"
92-
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
90+
let mediaFileIdentifier = "\(UUID().uuidString).\(String(mediaType))"
91+
let fileURL = temporaryFolderURL.appendingPathComponent(mediaFileIdentifier)
9392
try mediaData.write(to: fileURL)
9493

95-
return try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL)
94+
return try UNNotificationAttachment(identifier: mediaFileIdentifier, url: fileURL)
9695
} catch {
9796
return nil
9897
}

Sources/AmplifyUtilsNotifications/Payloads/PinpointNotificationPayload.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
struct PinpointNotificationPayload: AUNotificationPayload {
11-
var remoteImageURL: String? {
11+
var remoteMediaURL: String? {
1212
data.mediaURL
1313
}
1414

0 commit comments

Comments
 (0)