Skip to content

Instantly share code, notes, and snippets.

@BrandonSmith
Created June 22, 2018 13: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 BrandonSmith/4b7038d6ebc9146931b7b0a9cadac352 to your computer and use it in GitHub Desktop.
Save BrandonSmith/4b7038d6ebc9146931b7b0a9cadac352 to your computer and use it in GitHub Desktop.
fcm-apns-debugging
#!/bin/bash
curl=/usr/local/opt/curl/bin/curl
openssl=/usr/local/opt/openssl/bin/openssl
deviceToken=TOKEN
authKey="./AuthKey_##########.p8"
authKeyId=##########
teamId=##########
bundleId=$$$$$$$$$$$$$$
endpoint=https://api.development.push.apple.com
#endpoint=https://api.push.apple.com
read -r -d '' payload <<-'EOF'
{
"aps": {
"badge": 0,
"category": "mycategory",
"alert": {
"title": "my title",
"subtitle": "my subtitle",
"body": "my body text message"
}
},
"custom": {
"mykey": "myvalue"
}
}
EOF
base64() {
$openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
sign() {
printf "$1" | $openssl dgst -binary -sha256 -sign "$authKey" | base64
}
time=$(date +%s)
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
jwt="$header.$claims.$(sign $header.$claims)"
$curl --verbose \
--header "content-type: application/json" \
--header "authorization: bearer $jwt" \
--header "apns-topic: $bundleId" \
--data "$payload" \
$endpoint/3/device/$deviceToken
var { google } = require('googleapis')
function getAccessToken() {
return new Promise(function(resolve, reject) {
var key = require('./firebase-adminsdk-#####-##########.json');
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/firebase.messaging'],
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
reject(err);
return;
}
resolve(tokens.access_token);
});
});
}
getAccessToken().then(console.log)
{
"name": "fcm-apns-debugging",
"version": "1.0.0",
"description": "",
"main": "token.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Brandon Smith <brandon@16cards.com> (http://16cards.com/)",
"license": "ISC",
"dependencies": {
"googleapis": "~32.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment