Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the apns-unique-id header in APNSResponse #198

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/APNS/APNSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ extension APNSClient {
let response = try await self.httpClient.execute(httpClientRequest, deadline: .distantFuture)

let apnsID = response.headers.first(name: "apns-id").flatMap { UUID(uuidString: $0) }
let apnsUniqueID = response.headers.first(name: "apns-unique-id").flatMap { UUID(uuidString: $0) }

if response.status == .ok {
return APNSResponse(apnsID: apnsID)
return APNSResponse(apnsID: apnsID, apnsUniqueID: apnsUniqueID)
}

let body = try await response.body.collect(upTo: 1024)
Expand All @@ -203,6 +204,7 @@ extension APNSClient {
let error = APNSError(
responseStatus: Int(response.status.code),
apnsID: apnsID,
apnsUniqueID: apnsUniqueID,
apnsResponse: errorResponse,
timestamp: errorResponse.timestampInSeconds.flatMap { Date(timeIntervalSince1970: $0) }
)
Expand Down
9 changes: 9 additions & 0 deletions Sources/APNSCore/APNSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ public struct APNSError: Error {
/// Use this value to identify the notification. If you don’t specify an `apnsID` in your request,
/// APNs creates a new `UUID` and returns it in this header.
public let apnsID: UUID?

/// A unique ID for the notification used for development, as determined by the APNs servers.
///
/// In the development or sandbox environement, this value can be used to look up information about notifications on the [Push Notifications Console](https://icloud.developer.apple.com/dashboard/notifications). This value is not provided in the production environement.
public var apnsUniqueID: UUID?

/// The error code indicating the reason for the failure.
public let reason: ErrorReason?
Expand All @@ -405,11 +410,13 @@ public struct APNSError: Error {
public init(
responseStatus: Int,
apnsID: UUID? = nil,
apnsUniqueID: UUID? = nil,
apnsResponse: APNSErrorResponse? = nil,
timestamp: Date? = nil
) {
self.responseStatus = responseStatus
self.apnsID = apnsID
self.apnsUniqueID = apnsUniqueID
if let apnsResponse {
self.reason = .init(_reason: .init(rawValue: apnsResponse.reason))
} else {
Expand All @@ -425,13 +432,15 @@ extension APNSError: Hashable {
return
lhs.responseStatus == rhs.responseStatus &&
lhs.apnsID == rhs.apnsID &&
lhs.apnsUniqueID == rhs.apnsUniqueID &&
lhs.reason == rhs.reason &&
lhs.timestamp == rhs.timestamp
}

public func hash(into hasher: inout Hasher) {
hasher.combine(self.responseStatus)
hasher.combine(self.apnsID)
hasher.combine(self.apnsUniqueID)
hasher.combine(self.reason)
hasher.combine(self.timestamp)
}
Expand Down
16 changes: 15 additions & 1 deletion Sources/APNSCore/APNSResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@ public struct APNSResponse: Hashable {
/// Use this value to identify the notification. If you don’t specify an `apnsID` in your request,
/// APNs creates a new `UUID` and returns it in this header.
public var apnsID: UUID?

/// A unique ID for the notification used for development, as determined by the APNs servers.
///
/// In the development or sandbox environement, this value can be used to look up information about notifications on the [Push Notifications Console](https://icloud.developer.apple.com/dashboard/notifications). This value is not provided in the production environement.
public var apnsUniqueID: UUID?

/// Initializes a new ``APNSResponse``.
///
/// - Parameter apnsID: The same value as the `apnsID` send in the request.
public init(apnsID: UUID? = nil) {
/// - Parameter apnsUniqueID: A unique ID for the notification used only in the development environment.
public init(apnsID: UUID? = nil, apnsUniqueID: UUID? = nil) {
self.apnsID = apnsID
self.apnsUniqueID = apnsUniqueID
}
}

/// The [Push Notifications Console](https://icloud.developer.apple.com/dashboard/notifications) expects IDs to be lowercased, so prep them ahead of time here to make it easier for users to copy and paste these IDs.
extension APNSResponse: CustomStringConvertible {
public var description: String {
"APNSResponse(apns-id: \(apnsID?.uuidString.lowercased() ?? "nil"), apns-unique-id: \(apnsUniqueID?.uuidString.lowercased() ?? "nil"))"
}
}
4 changes: 3 additions & 1 deletion Sources/APNSURLSession/APNSUrlSessionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ public struct APNSURLSessionClient: APNSClientProtocol {
}

let apnsID = UUID(uuidString: apnsIDString)
let apnsUniqueID = (response.allHeaderFields["apns-unique-id"] as? String).flatMap { UUID(uuidString: $0) }

/// Detect an error
if let errorResponse = try? decoder.decode(APNSErrorResponse.self, from: data) {
let error = APNSError(
responseStatus: response.statusCode,
apnsID: apnsID,
apnsUniqueID: apnsUniqueID,
apnsResponse: errorResponse,
timestamp: errorResponse.timestampInSeconds.flatMap { Date(timeIntervalSince1970: $0) }
)
throw error
} else {
/// Return APNSResponse
return APNSResponse(apnsID: apnsID)
return APNSResponse(apnsID: apnsID, apnsUniqueID: apnsUniqueID)
}
}
}
Expand Down