Guest User

Untitled

a guest
Nov 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. const apn = require('apn')
  2.  
  3. const OPTIONS = {
  4. cert: "./PushNotificationAlert-cert.pem",
  5. key: "./PushNotificationAlert-key.pem",
  6. production: false
  7. }
  8. const MESSAGES = {"DOUBLE": "お腹がすいた!", "LONG": "たすけてぇ〜"}
  9.  
  10. exports.handler = (event, context, callback) => {
  11. const apnProvider = new apn.Provider(OPTIONS)
  12. const deviceToken = process.env["TOKEN"]
  13. const hour = (new Date()).getHours()
  14.  
  15. let message = "ヘロヘロヘ"
  16. if (event.deviceEvent) {
  17. message = MESSAGES[event.deviceEvent.buttonClicked.clickType]
  18. if (!message) {
  19. if (hour < 12) {
  20. message = "おはようございます。"
  21. } else if (hour < 18) {
  22. message = "こんにちは。"
  23. } else {
  24. message = "こんばんは。"
  25. }
  26. }
  27. } else {
  28. message = "テスト(Lambda)"
  29. }
  30.  
  31. const note = new apn.Notification()
  32. note.badge = hour
  33. note.sound = "siren.aiff"
  34. note.alert = message
  35. note.payload = {data: event}
  36.  
  37. apnProvider.send(note, deviceToken).then( (result) => {
  38. console.log(result.failed)
  39. callback(null, "OK")
  40. apnProvider.client.endpointManager._endpoints.forEach(endpoint => endpoint.destroy())
  41. })
  42. apnProvider.shutdown()
  43. }
Add Comment
Please, Sign In to add comment