Smartthings SmartApp send downstream cloud message with Google Firebase

smartthings-logo-horizontal.b229e786

  1. Follow instruction at Smartthings to create and configure the SmartApp.
  2. Follow the document to subscribe the things event. An example at github shows a sample code snippet of a working SmartApp – provides a HTTP GET API interface to list all the available things and the data.
  3. Also, follow the Firebase document to setup the client device SDK and note down the client device token, and the firebase server key.
  4. Write an event data handler to process the event and send downstream message by http POST. Sample code can be seen below.
/*
Example of Smartthings SmartApp to send downstream cloud message with Firebase
Event subscription is needed to trigger the event handler
*/
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
/* Subscribe the event to the handler */
def initialize() {
subscribe(switches,"switch",sendEventToFirebase)
}
/* Send event data to Google Firebase from http POST */
def sendEventToFirebase(evt){
def serverKey = "Firebase Server Key"
def clientToken = "Client Device Token"
def params = [
uri: "https://fcm.googleapis.com/fcm/send",
headers: [
Authorization: "key=" + serverKey
],
body: [
to: clientToken,
data: [
deviceId: evt.deviceId,
eventName: evt.name,
value: evt.stringValue]
]
]
try {
httpPostJson(params) { resp ->
resp.headers.each {
//log.debug "${it.name} : ${it.value}"
}
log.debug "DEBUG (POST FIREBASE): response contentType: ${resp. contentType}"
}
} catch (e) {
log.debug "something went wrong: $e"
}
}

2 thoughts on “Smartthings SmartApp send downstream cloud message with Google Firebase

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.