为ChatSecure-Push-Server提供的iOS SDK。
pod 'ChatSecure-Push-iOS', :git => 'https://github.com/ChatSecure/ChatSecure-Push-iOS'
如果是首次启动且没有账户。
let client = Client(baseUrl: NSURL(string: urlString)!, urlSessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration(), account: nil)
或者如果您已经有账户。您需要存储用户名
和令牌
以进行API请求。
let client = Client(baseUrl: NSURL(string: urlString)!, urlSessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration(), account: account)
client.registerNewUser(username, password: password, email: nil, completion: { (account, error) -> Void in
//Save account here
})
为了注册新设备,首先获取一个APNS令牌。
var settings = UIUserNotificationSettings(forTypes: (UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert), categories: nil)
application.registerUserNotificationSettings(settings)
一旦您在AppDelegate
中收到令牌。
self.client.registerDevice(apnsToken, name: "New device name", deviceID: nil, completion: { (device, error) -> Void in
//Save device here
})
self.client.createToken(apnsToken, name: "New token", completion: { (token, error) -> Void in
//Save and send to friend
})
一旦您从朋友那里收到一个令牌
,您可以向他们发送推送消息。
data
需要一个可序列化为JSON的字典。
var message = Message(token: token, data: nil)
self.client.sendMessage(message, completion: { (msg, error) -> Void in
println("Message: \(msg)")
})