Skip to content

Instantly share code, notes, and snippets.

@mgwedd
Created July 16, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgwedd/57ca56151a37ffdfd2619bad8a807515 to your computer and use it in GitHub Desktop.
Save mgwedd/57ca56151a37ffdfd2619bad8a807515 to your computer and use it in GitHub Desktop.
func registerSubscriptions() {
func messageReceived(payload: Data) {
let payloadDictionary = jsonDataToDict(jsonData: payload)
print("Message received: \(payloadDictionary)")
// Handle message event here...
}
let topicArray = ["topicOne", "topicTwo", "topicThree"]
let dataManager = AWSIoTDataManager(forKey: "kDataManager")
for topic in topicArray {
print("Registering subscription to => \(topic)")
dataManager.subscribe(toTopic: topic,
qoS: .messageDeliveryAttemptedAtLeastOnce, // Set according to use case
messageCallback: messageReceived)
}
}
func jsonDataToDict(jsonData: Data?) -> Dictionary <String, Any> {
// Converts data to dictionary or nil if error
do {
let jsonDict = try JSONSerialization.jsonObject(with: jsonData!, options: [])
let convertedDict = jsonDict as! [String: Any]
return convertedDict
} catch {
// Couldn't get JSON
print(error.localizedDescription)
return [:]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment