Skip to content

Commit

Permalink
✨ Add push test send trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt authored and iujames committed Sep 17, 2024
1 parent 7dfb993 commit f9af149
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Sources/AppcuesKit/Data/Networking/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal enum APIEndpoint: Endpoint {
case qualify(userID: String)
case content(experienceID: String, queryItems: [URLQueryItem] = [])
case preview(experienceID: String, queryItems: [URLQueryItem] = [])
case pushTest
case health

/// URL fragments that that are appended to the `Config.apiHost` to make the URL for a network request.
Expand All @@ -36,6 +37,8 @@ internal enum APIEndpoint: Endpoint {
components.path = "/v1/accounts/\(config.accountID)/users/\(storage.userID)/experience_preview/\(experienceID)"
}
components.queryItems = queryItems
case .pushTest:
components.path = "/v1/accounts/\(config.accountID)/push_notification_test"
case .health:
components.path = "/healthz"
}
Expand Down
51 changes: 47 additions & 4 deletions Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Combine

@available(iOS 13.0, *)
internal class PushVerifier {
enum ErrorMessage: CustomStringConvertible {
enum ErrorMessage: Equatable, CustomStringConvertible {
case noToken
case notAuthorized
case permissionDenied
Expand All @@ -21,6 +21,8 @@ internal class PushVerifier {
case multipleCompletions
case noSDKResponse

case serverError(String)

// Verification flow errors
case tokenMismatch
case responseInitFail
Expand All @@ -43,6 +45,8 @@ internal class PushVerifier {
return "Error 7: Receive completion called too many times"
case .noSDKResponse:
return "Error 8: Receive response not passed to SDK"
case .serverError:
return "Error 9: Server Error"
case .tokenMismatch:
return "Error 10: Unexpected result"
case .responseInitFail:
Expand Down Expand Up @@ -175,13 +179,52 @@ internal class PushVerifier {
}

private func verifyServerComponents(token: String) {
// TODO: trigger remote call to verify E2E
if errors.isEmpty {
subject.send(StatusItem(status: .verified, title: PushVerifier.title))
let body = PushTest(
deviceID: storage.deviceID
)

let data = try? NetworkClient.encoder.encode(body)

networking.post(
to: APIEndpoint.pushTest,
authorization: nil,
body: data,
requestId: nil
) { [weak self] (result: Result<PushTestResponse, Error>) in
DispatchQueue.main.async {
switch result {
case .success:
if self?.errors.isEmpty == true {
self?.subject.send(StatusItem(status: .verified, title: PushVerifier.title))
}
case .failure(let error):
self?.errors.append(.serverError(error.localizedDescription))
}
}
}
}
}

@available(iOS 13.0, *)
private extension PushVerifier {
struct PushTest: Encodable {
let deviceID: String

enum CodingKeys: String, CodingKey {
case deviceID = "device_id"
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.deviceID, forKey: .deviceID)
}
}

struct PushTestResponse: Decodable {
let ok: Bool
}
}

private extension UNNotificationResponse {
final class KeyedArchiver: NSKeyedArchiver {
override func decodeObject(forKey _: String) -> Any { "" }
Expand Down

0 comments on commit f9af149

Please sign in to comment.