Skip to content

Commit b06c29e

Browse files
committed
Added error handling to provider
1 parent 948a2dd commit b06c29e

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

Sources/SwiftyAPNS/Notification.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import Foundation
1010

1111
public struct APNSNotification {
1212
/// The Remote Notification Payload.
13-
public let payload: Payload
13+
public var payload: Payload
1414

1515
/// Specify the hexadecimal string of the device token for the target device.
16-
public let token: String
16+
public var token: String
1717

1818
/// The optional settings for the notification
19-
public let options: NotificationOptions
19+
public var options: NotificationOptions
2020

2121
public init(payload: Payload, token: String, options: NotificationOptions = NotificationOptions.default) {
2222
self.payload = payload

Sources/SwiftyAPNS/Payload.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ public class Payload: Encodable {
2626
self.aps = aps
2727
}
2828

29-
public convenience init(alert: APSAlert) {
30-
self.init(alert: alert, badge: nil, sound: nil, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
31-
}
32-
33-
public convenience init(alert: APSAlert?, badge: Int? = 0) {
34-
self.init(alert: alert, badge: badge, sound: nil, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
35-
}
36-
3729
public convenience init(alert: APSAlert?, badge: Int? = 0, sound: String? = "default") {
3830
self.init(alert: alert, badge: badge, sound: sound, contentAvailable: nil, mutableContent: nil, category: nil, threadId: nil)
3931
}
@@ -45,6 +37,10 @@ public class Payload: Encodable {
4537
public static var background: Payload {
4638
return Payload(alert: nil, badge: nil, sound: nil, contentAvailable: 1, mutableContent: nil, category: nil, threadId: nil)
4739
}
40+
41+
public static var mutable: Payload {
42+
return Payload(alert: nil, badge: nil, sound: nil, contentAvailable: 0, mutableContent: 1, category: nil, threadId: nil)
43+
}
4844
}
4945

5046
/// The APS can contain one or more properties that specify the following user notification types:

Sources/SwiftyAPNS/Provider.swift

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,39 @@
88

99
import Foundation
1010

11-
public class Provider: NSObject {
11+
public enum APNSProviderError: Error {
12+
13+
case BadUrl
14+
case EncodePayload
15+
case ParseResponce
16+
case EmptyData
17+
18+
public var description: String {
19+
switch self {
20+
case .BadUrl: return
21+
"The url was invalid"
22+
case .EncodePayload: return
23+
"Can't encode payload"
24+
case .ParseResponce: return
25+
"Can't parse responce"
26+
case .EmptyData: return
27+
"Empty data"
28+
}
29+
}
30+
}
31+
32+
public class APNSProvider: NSObject {
1233

1334
private var identity: SecIdentity
1435
private var sesion: URLSession?
1536

16-
public init(identity: SecIdentity, sandbox: Bool = true, qeue: OperationQueue = OperationQueue.main) {
37+
public init(identity: SecIdentity, sandbox: Bool = true, configuration: URLSessionConfiguration = URLSessionConfiguration.default, qeue: OperationQueue = OperationQueue.main) {
1738
self.identity = identity
18-
1939
super.init()
20-
21-
let configuration = URLSessionConfiguration.default
2240
self.sesion = URLSession.init(configuration: configuration, delegate: self, delegateQueue: qeue)
2341
}
2442

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

2745
let options = notification.options
2846
var components = URLComponents()
@@ -33,7 +51,7 @@ public class Provider: NSObject {
3351
components.port = port.rawValue
3452
}
3553
guard let url = components.url else {
36-
// TODO: completion(.failure(APNSError))
54+
completion(.failure(APNSProviderError.BadUrl))
3755
return
3856
}
3957
var request = URLRequest.init(url: url)
@@ -65,13 +83,13 @@ public class Provider: NSObject {
6583
request.httpBody = payload
6684
}
6785
catch {
68-
// TODO: completion(.failure(APNSError))
86+
completion(.failure(APNSProviderError.EncodePayload))
6987
return
7088
}
7189

7290
let task = self.sesion?.dataTask(with: request) { (data, responce, error) in
73-
if let _ = error {
74-
// TODO: completion(.failure(APNSError))
91+
if let error = error {
92+
completion(.failure(error))
7593
} else if let responce = responce as? HTTPURLResponse, let data = data {
7694
if let apnsStatus = APNSStatus(code: responce.statusCode),
7795
let apnsId = responce.allHeaderFields["apns-id"] as? String
@@ -82,19 +100,18 @@ public class Provider: NSObject {
82100
completion(.success(apnsResponce))
83101
}
84102
else {
85-
// TODO: completion(.failure(APNSError)) // error cant parse responce
103+
completion(.failure(APNSProviderError.ParseResponce))
86104
}
87105
}
88106
else {
89-
// TODO: completion(.failure(APNSError)) // error empty data
107+
completion(.failure(APNSProviderError.EmptyData))
90108
}
91109
}
92-
93110
task?.resume()
94111
}
95112
}
96113

97-
extension Provider: URLSessionDelegate {
114+
extension APNSProvider: URLSessionDelegate {
98115
public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
99116
if let error = error {
100117
print("Error: \(error)")

Tests/SwiftyAPNSTests/SwiftyAPNSTests.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class CustomPayload3: Payload {
7474
final class SwiftyAPNSTests: XCTestCase {
7575
let token = "0ab0aaff76ab302ecba6e28fddcc457c8e9c12f6cff68d9fecdce2b6df1f1177"
7676
let topic = "com.push.example"
77-
let pushCertPath = "./dev_push_cert.p12"
77+
let pushCertPath = "./push_cert.p12"
7878
let pushPassword = "secure"
7979

8080
func testAlertPushExample() {
@@ -174,16 +174,28 @@ final class SwiftyAPNSTests: XCTestCase {
174174
XCTFail("Fail"); return
175175
}
176176

177-
let expect = self.expectation(description: "apnsExpectation")
178-
let provider = Provider.init(identity: identity)
179-
provider.push(notification) { (response, error) in
180-
XCTAssertTrue(error == nil, "No error")
181-
expect.fulfill()
177+
let expect = self.expectation(description: "APNSExpectation")
178+
let provider = APNSProvider.init(identity: identity)
179+
provider.push(notification) { (result) in
180+
switch(result) {
181+
case .success(let responce):
182+
if let error = responce.reason {
183+
XCTFail(error.description)
184+
} else {
185+
expect.fulfill()
186+
}
187+
case .failure(let error):
188+
if let error = error as? APNSProviderError {
189+
XCTFail(error.description)
190+
} else {
191+
XCTFail(error.localizedDescription)
192+
}
193+
}
182194
}
183195

184196
self.waitForExpectations(timeout: 5.0) { (error) in
185197
if let error = error {
186-
print("Error \(error)")
198+
XCTFail(error.localizedDescription)
187199
}
188200
}
189201
}

0 commit comments

Comments
 (0)