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