Skip to content

Commit 180ddc4

Browse files
authored
Merge pull request #111 from pusher/add-register-fail-delegate-func
Add failedToRegisterForPushNotifications func to PusherDelegate.
2 parents 3012eb9 + 479ff38 commit 180ddc4

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,13 +925,15 @@ You can also implement some of the `PusherDelegate` functions to get access to e
925925
926926
```swift
927927
@objc optional func registeredForPushNotifications(clientId: String)
928+
@objc optional func failedToRegisterForPushNotifications(response: URLResponse, responseBody: String?)
928929
@objc optional func subscribedToInterest(name: String)
929930
@objc optional func unsubscribedFromInterest(name: String)
930931
```
931932

932933
Again, the names of the functions largely give away what their purpose is but just for completeness:
933934

934935
- `registeredForPushNotifications` - use this if you want to know when a client has successfully registered with the Pusher Push Notifications service, or if you want access to the `clientId` that is returned upon successful registration
936+
- `failedToRegisterForPushNotifications` - use this if you want to know when a client has failed to register with the Pusher Push Notifications service
935937
- `subscribedToInterest` - use this if you want keep track of interests that are successfully subscribed to
936938
- `unsubscribedFromInterest` - use this if you want keep track of interests that are successfully unsubscribed from
937939

Source/NativePusher.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
} else {
136136
if data != nil && response != nil {
137137
let responseBody = String(data: data!, encoding: .utf8)
138+
self.delegate?.failedToRegisterForPushNotifications?(response: response!, responseBody: responseBody)
138139
self.delegate?.debugLog?(message: "Bad HTTP response: \(response!) with body: \(responseBody)")
139140
}
140141
}

Source/PusherDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@objc optional func debugLog(message: String)
1111

1212
@objc optional func registeredForPushNotifications(clientId: String)
13+
@objc optional func failedToRegisterForPushNotifications(response: URLResponse, responseBody: String?)
1314
@objc optional func subscribedToInterest(name: String)
1415
@objc optional func unsubscribedFromInterest(name: String)
1516

Tests/NativePusherTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class NativePusherTests: XCTestCase {
3434
public var registerEx: XCTestExpectation? = nil
3535
public var subscribeEx: XCTestExpectation? = nil
3636
public var unsubscribeEx: XCTestExpectation? = nil
37+
public var registerFailEx: XCTestExpectation? = nil
3738
public var interestName: String? = nil
3839

3940
public func subscribedToInterest(name: String) {
@@ -52,6 +53,10 @@ class NativePusherTests: XCTestCase {
5253
XCTAssertEqual(clientId, testClientId)
5354
registerEx!.fulfill()
5455
}
56+
57+
public func failedToRegisterForPushNotifications(response: URLResponse, responseBody: String?) {
58+
registerFailEx!.fulfill()
59+
}
5560
}
5661

5762
var key: String!
@@ -136,6 +141,20 @@ class NativePusherTests: XCTestCase {
136141
XCTAssertEqual(pusher.nativePusher.requestQueue.count, 1, "the nativePusher request queue should contain the subscribe request")
137142
XCTAssertEqual(pusher.nativePusher.requestQueue.paused, true, "the nativePusher request queue should be paused")
138143
}
144+
145+
func testFailingToRegisterWithPusherForPushNotificationsCallsTheAppropriateDelegateFunction() {
146+
let jsonData = "".data(using: String.Encoding.utf8, allowLossyConversion: false)!
147+
let url = URL(string: "https://nativepushclient-cluster1.pusher.com/client_api/v1/clients")!
148+
let urlResponse = HTTPURLResponse(url: url, statusCode: 500, httpVersion: nil, headerFields: nil)
149+
MockSession.addMockResponse(for: url, httpMethod: "POST", data: jsonData, urlResponse: urlResponse, error: nil)
150+
151+
let registerFailEx = expectation(description: "the appropriate delegate should be called when registration fails")
152+
dummyDelegate.registerFailEx = registerFailEx
153+
154+
pusher.nativePusher.register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
155+
156+
waitForExpectations(timeout: 0.5)
157+
}
139158
}
140159

141160
#endif

0 commit comments

Comments
 (0)