@@ -10,14 +10,21 @@ import Foundation
10
10
Use the Pusher.nativePusher() method to get access to it.
11
11
*/
12
12
public class NativePusher {
13
+ static let sharedInstance = NativePusher ( )
14
+
13
15
private static let PLATFORM_TYPE = " apns "
14
16
private let CLIENT_API_V1_ENDPOINT = " https://nativepushclient-cluster1.pusher.com/client_api/v1 "
15
17
16
18
private let URLSession = NSURLSession . sharedSession ( )
17
19
18
20
// Identifies a Pusher app.
19
21
// This app should have push notifications enabled.
20
- private let pusherAppKey : String
22
+ private var pusherAppKey : String ?
23
+
24
+ public func setPusherAppKey( pusherAppKey: String ) {
25
+ self . pusherAppKey = pusherAppKey
26
+ tryFlushOutbox ( )
27
+ }
21
28
22
29
// The id issued to this app instance by Pusher.
23
30
// We get it upon registration.
@@ -27,10 +34,8 @@ public class NativePusher {
27
34
// Queued actions to perform when the client is registered.
28
35
private var outbox : Array < ( String , SubscriptionChange ) >
29
36
30
- // Expected to be initialized by instances of Pusher.
31
37
// Normal clients should access the shared instance via Pusher.nativePusher().
32
- public init ( pusherAppKey: String ) {
33
- self . pusherAppKey = pusherAppKey
38
+ public init ( ) {
34
39
clientId = nil
35
40
outbox = [ ]
36
41
}
@@ -114,15 +119,19 @@ public class NativePusher {
114
119
}
115
120
116
121
private func tryFlushOutbox( ) {
117
- if ( self . clientId != nil && 0 < outbox. count) {
118
- let ( interest, change) = outbox. removeAtIndex ( 0 )
119
- modifySubscription ( interest, change: change) {
120
- self . tryFlushOutbox ( )
122
+ switch ( self . pusherAppKey, self . clientId) {
123
+ case ( . Some( let pusherAppKey) , . Some( let clientId) ) :
124
+ if ( self . pusherAppKey != nil && self . clientId != nil && 0 < outbox. count) {
125
+ let ( interest, change) = outbox. removeAtIndex ( 0 )
126
+ modifySubscription ( pusherAppKey, clientId: clientId, interest: interest, change: change) {
127
+ self . tryFlushOutbox ( )
128
+ }
121
129
}
130
+ case _: break
122
131
}
123
132
}
124
133
125
- private func modifySubscription( interest: String , change: SubscriptionChange , callback: ( Void ) -> ( Void ) ) {
134
+ private func modifySubscription( pusherAppKey : String , clientId : String , interest: String , change: SubscriptionChange , callback: ( Void ) -> ( Void ) ) {
126
135
let url = " \( CLIENT_API_V1_ENDPOINT) /clients/ \( clientId) /interests/ \( interest) "
127
136
let request = NSMutableURLRequest ( URL: NSURL ( string: url) !)
128
137
switch ( change) {
@@ -133,7 +142,7 @@ public class NativePusher {
133
142
}
134
143
135
144
let params : [ String : AnyObject ] = [
136
- " app_key " : self . pusherAppKey,
145
+ " app_key " : pusherAppKey,
137
146
]
138
147
139
148
try ! request. HTTPBody = NSJSONSerialization . dataWithJSONObject ( params, options: [ ] )
0 commit comments