Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Last active December 29, 2017 08:07
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 nathanborror/8489b6008dd422e3362fba4d78b603f6 to your computer and use it in GitHub Desktop.
Save nathanborror/8489b6008dd422e3362fba4d78b603f6 to your computer and use it in GitHub Desktop.
var jwt = require("jsonwebtoken")
var http2 = require("http2")
const ALGORITHM = "ES256"
const APNS_KEY_ID = "YOUR_APNS_KEY_ID"
const TEAM_ID = "YOUR_TEAM_ID"
const BUNDLE_ID = "YOUR_BUNDLE_ID"
const REGISTRATION_ID = "YOUR_DEVICE_PUSH_TOKEN"
const CERT =
"-----BEGIN PRIVATE KEY-----\n\
-----END PRIVATE KEY-----"
// subscribe is the main function called by Cloud Functions.
module.exports.statusSubscribe = (event, callback) => {
const build = eventToBuild(event.data.data)
// Skip if the current status is not in the status list.
// Add additional statues to list if you'd like:
// QUEUED, WORKING, SUCCESS, FAILURE,
// INTERNAL_ERROR, TIMEOUT, CANCELLED
const status = ["SUCCESS", "FAILURE", "INTERNAL_ERROR", "TIMEOUT"]
if (status.indexOf(build.status) === -1) {
return callback()
}
const payload = { aps: { alert: `${build.status}` } }
const token = jwt.sign({ iss: TEAM_ID, iat: Math.floor(Date.now() / 1000) - 1 }, CERT, {
algorithm: ALGORITHM,
header: { alg: ALGORITHM, kid: APNS_KEY_ID }
})
const path = `/3/device/${REGISTRATION_ID}`
const options = {}
options.host = "api.development.push.apple.com"
options.port = 443
options.method = "POST"
options.path = path
options.headers = {}
options.headers["apns-expiration"] = "0"
options.headers["apns-priority"] = "10"
options.headers["apns-topic"] = BUNDLE_ID
options.headers["authorization"] = `bearer ${token}`
const req = http2.request(options, res => {
let data = []
res.on("data", chunks => {
data.push(chunks.toString("utf8"))
})
res.on("end", () => {
console.log(data)
console.log(res)
})
})
req.on("error", err => {
console.error(err)
})
req.write(JSON.stringify(payload))
req.end()
}
// eventToBuild transforms pubsub event message to a build object.
const eventToBuild = data => {
return JSON.parse(new Buffer(data, "base64").toString())
}
{
"name": "google-container-status",
"version": "0.0.1",
"description": "Status integration for Google Cloud Container Builder, using Google Cloud Functions",
"main": "index.js",
"dependencies": {
"jsonwebtoken": "8.1.0",
"http2": ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment