|
/* |
|
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" |
|
} |
|
|
|
} |