Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
clientId=$(hostname)
topic="no topic"
payload="no payload"
title="$(hostname) Detection"
startMillis=$(date +'%s')
startDate=$(date '+%Y-%m-%d %H:%M:%S')
SERVER_KEY=GCM-SERVERKEY # you find it in firebase console
while getopts ":c:p:w:q:t:" opt; do
case $opt in
c) clientId="$OPTARG"
;;
p) payload="$OPTARG"
;;
t) topic="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
eval "action=''"
eval "notification='\"notification\":{\"body\":\"$payload\",\"title\":\"$title\"$action}'"
eval "data='{\"to\" : \"/topics/${topic}\", \"data\":{\"message\":\"$payload\",\"start\": $startMillis,\"date\":\"$startDate\",\"client\":\"$clientId\"}, $notification}'"
echo $data > /tmp/json
eval "cmd='curl -X POST --header \"Authorization: key=$SERVER_KEY\" --Header \"Content-Type: application/json\" https://fcm.googleapis.com/fcm/send -d \"@/tmp/json\"'"
result=$(curl -X POST --header "Authorization: key=$SERVER_KEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "@/tmp/json")
echo $result
@hannesa2
Copy link
Author

hannesa2 commented Mar 8, 2018

./sendNotification.sh -t test -p myPayload

fires something like

"to": "/topics/test",
 "data": {
   "message": "myPayload",
   "start": 1519153575,
   "date": "2018-02-20 20:06:15",
   "client": "pi3"
 },
 "notification": {
   "body": "myPayload",
   "title": "pi3 Detection"
 }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment