Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) => {
- const apnProvider = new apn.Provider(OPTIONS)
- const deviceToken = process.env["TOKEN"]
- const hour = (new Date()).getHours()
- let message = "ヘロヘロヘ"
- if (event.deviceEvent) {
- message = MESSAGES[event.deviceEvent.buttonClicked.clickType]
- if (!message) {
- if (hour < 12) {
- message = "おはようございます。"
- } else if (hour < 18) {
- message = "こんにちは。"
- } else {
- message = "こんばんは。"
- }
- }
- } else {
- message = "テスト(Lambda)"
- }
- 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()
- }
Add Comment
Please, Sign In to add comment