Skip to content

Instantly share code, notes, and snippets.

@randymxj
Last active July 10, 2016 21:16
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 randymxj/76a68906450c80e754a087004cbfa3ef to your computer and use it in GitHub Desktop.
Save randymxj/76a68906450c80e754a087004cbfa3ef to your computer and use it in GitHub Desktop.
Sending JSON payload from Smartthings SmartApp to Google Firebase Cloud Message
/*
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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment