Skip to content

Instantly share code, notes, and snippets.

@yuumi3
Last active November 20, 2018 01:05
Show Gist options
  • Save yuumi3/a8bf80bd0d22d3f0d6d7985dc9ef2f5f to your computer and use it in GitHub Desktop.
Save yuumi3/a8bf80bd0d22d3f0d6d7985dc9ef2f5f to your computer and use it in GitHub Desktop.
const apn = require('apn')
const OPTIONS = {
cert: "./PushNotificationAlert-cert.pem",
key: "./PushNotificationAlert-key.pem",
production: false
}
const MESSAGES = {"DOUBLE": "お腹がすいた!", "LONG": "たすけてぇ〜"}
exports.handler = (event, context, callback) => {
let message = ""
if (event.deviceEvent) {
const hour = (new Date()).getHours()
message = MESSAGES[event.deviceEvent.buttonClicked.clickType]
if (!message) {
if (hour < 12) {
message = "おはようございます。"
} else if (hour < 18) {
message = "こんにちは。"
} else {
message = "こんばんは。"
}
}
} else {
message = "テスト(Lambda)"
}
const apnProvider = new apn.Provider(OPTIONS)
const deviceToken = process.env["TOKEN"]
const note = new apn.Notification()
note.badge = hour
note.sound = "siren.aiff"
note.alert = message
note.payload = {data: event}
apnProvider.send(note, deviceToken).then(result => {
console.log(result.failed)
callback(null, "OK")
apnProvider.client.endpointManager._endpoints.forEach(endpoint => endpoint.destroy())
})
apnProvider.shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment