-
-
Save mgwedd/57ca56151a37ffdfd2619bad8a807515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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