Last active
July 10, 2016 21:16
-
-
Save randymxj/76a68906450c80e754a087004cbfa3ef to your computer and use it in GitHub Desktop.
Sending JSON payload from Smartthings SmartApp to Google Firebase Cloud Message
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
/* | |
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