Skip to content

Instantly share code, notes, and snippets.

@hannesa2
Last active March 8, 2018 15:44
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 hannesa2/2a898ad6b08db5aa8a86b3dce28f7887 to your computer and use it in GitHub Desktop.
Save hannesa2/2a898ad6b08db5aa8a86b3dce28f7887 to your computer and use it in GitHub Desktop.
#!/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