Skip to content

Commit

Permalink
Added error handling to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
polyitan committed Feb 25, 2020
1 parent 948a2dd commit b06c29e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Sources/SwiftyAPNS/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Foundation

public struct APNSNotification {
/// The Remote Notification Payload.
public let payload: Payload
public var payload: Payload

/// Specify the hexadecimal string of the device token for the target device.
public let token: String
public var token: String

/// The optional settings for the notification
public let options: NotificationOptions
public var options: NotificationOptions

public init(payload: Payload, token: String, options: NotificationOptions = NotificationOptions.default) {
self.payload = payload
Expand Down
12 changes: 4 additions & 8 deletions Sources/SwiftyAPNS/Payload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public class Payload: Encodable {
self.aps = aps
}

public convenience init(alert: APSAlert) {
self.init(alert: alert, badge: nil, sound: nil, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
}

public convenience init(alert: APSAlert?, badge: Int? = 0) {
self.init(alert: alert, badge: badge, sound: nil, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
}

public convenience init(alert: APSAlert?, badge: Int? = 0, sound: String? = "default") {
self.init(alert: alert, badge: badge, sound: sound, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
}
Expand All @@ -45,6 +37,10 @@ public class Payload: Encodable {
public static var background: Payload {
return Payload(alert: nil, badge: nil, sound: nil, contentAvailable: 1, mutableContent: nil, category: nil, threadId: nil)
}

public static var mutable: Payload {
return Payload(alert: nil, badge: nil, sound: nil, contentAvailable: 0, mutableContent: 1, category: nil, threadId: nil)
}
}

/// The APS can contain one or more properties that specify the following user notification types:
Expand Down
45 changes: 31 additions & 14 deletions Sources/SwiftyAPNS/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,39 @@

import Foundation

public class Provider: NSObject {
public enum APNSProviderError: Error {

case BadUrl
case EncodePayload
case ParseResponce
case EmptyData

public var description: String {
switch self {
case .BadUrl: return
"The url was invalid"
case .EncodePayload: return
"Can't encode payload"
case .ParseResponce: return
"Can't parse responce"
case .EmptyData: return
"Empty data"
}
}
}

public class APNSProvider: NSObject {

private var identity: SecIdentity
private var sesion: URLSession?

public init(identity: SecIdentity, sandbox: Bool = true, qeue: OperationQueue = OperationQueue.main) {
public init(identity: SecIdentity, sandbox: Bool = true, configuration: URLSessionConfiguration = URLSessionConfiguration.default, qeue: OperationQueue = OperationQueue.main) {
self.identity = identity

super.init()

let configuration = URLSessionConfiguration.default
self.sesion = URLSession.init(configuration: configuration, delegate: self, delegateQueue: qeue)
}

public func push(_ notification: APNSNotification, completion: @escaping (Result<APNSResponse, APNSError>) -> Void) {
public func push(_ notification: APNSNotification, completion: @escaping (Result<APNSResponse, Error>) -> Void) {

let options = notification.options
var components = URLComponents()
Expand All @@ -33,7 +51,7 @@ public class Provider: NSObject {
components.port = port.rawValue
}
guard let url = components.url else {
// TODO: completion(.failure(APNSError))
completion(.failure(APNSProviderError.BadUrl))
return
}
var request = URLRequest.init(url: url)
Expand Down Expand Up @@ -65,13 +83,13 @@ public class Provider: NSObject {
request.httpBody = payload
}
catch {
// TODO: completion(.failure(APNSError))
completion(.failure(APNSProviderError.EncodePayload))
return
}

let task = self.sesion?.dataTask(with: request) { (data, responce, error) in
if let _ = error {
// TODO: completion(.failure(APNSError))
if let error = error {
completion(.failure(error))
} else if let responce = responce as? HTTPURLResponse, let data = data {
if let apnsStatus = APNSStatus(code: responce.statusCode),
let apnsId = responce.allHeaderFields["apns-id"] as? String
Expand All @@ -82,19 +100,18 @@ public class Provider: NSObject {
completion(.success(apnsResponce))
}
else {
// TODO: completion(.failure(APNSError)) // error cant parse responce
completion(.failure(APNSProviderError.ParseResponce))
}
}
else {
// TODO: completion(.failure(APNSError)) // error empty data
completion(.failure(APNSProviderError.EmptyData))
}
}

task?.resume()
}
}

extension Provider: URLSessionDelegate {
extension APNSProvider: URLSessionDelegate {
public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
if let error = error {
print("Error: \(error)")
Expand Down
26 changes: 19 additions & 7 deletions Tests/SwiftyAPNSTests/SwiftyAPNSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class CustomPayload3: Payload {
final class SwiftyAPNSTests: XCTestCase {
let token = "0ab0aaff76ab302ecba6e28fddcc457c8e9c12f6cff68d9fecdce2b6df1f1177"
let topic = "com.push.example"
let pushCertPath = "./dev_push_cert.p12"
let pushCertPath = "./push_cert.p12"
let pushPassword = "secure"

func testAlertPushExample() {
Expand Down Expand Up @@ -174,16 +174,28 @@ final class SwiftyAPNSTests: XCTestCase {
XCTFail("Fail"); return
}

let expect = self.expectation(description: "apnsExpectation")
let provider = Provider.init(identity: identity)
provider.push(notification) { (response, error) in
XCTAssertTrue(error == nil, "No error")
expect.fulfill()
let expect = self.expectation(description: "APNSExpectation")
let provider = APNSProvider.init(identity: identity)
provider.push(notification) { (result) in
switch(result) {
case .success(let responce):
if let error = responce.reason {
XCTFail(error.description)
} else {
expect.fulfill()
}
case .failure(let error):
if let error = error as? APNSProviderError {
XCTFail(error.description)
} else {
XCTFail(error.localizedDescription)
}
}
}

self.waitForExpectations(timeout: 5.0) { (error) in
if let error = error {
print("Error \(error)")
XCTFail(error.localizedDescription)
}
}
}
Expand Down

0 comments on commit b06c29e

Please sign in to comment.