sending-to-multiple-devices.js 982 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. Send an identical notification to multiple devices.
  3. Possible use cases:
  4. - Breaking news
  5. - Announcements
  6. - Sport results
  7. */
  8. const apn = require('@parse/node-apn');
  9. const tokens = ['<insert token here>', '<insert token here>'];
  10. const service = new apn.Provider({
  11. cert: 'certificates/cert.pem',
  12. key: 'certificates/key.pem',
  13. });
  14. const note = new apn.Notification({
  15. alert: 'Breaking News: I just sent my first Push Notification',
  16. });
  17. // The topic is usually the bundle identifier of your application.
  18. note.topic = '<bundle identifier>';
  19. console.log(`Sending: ${note.compile()} to ${tokens}`);
  20. service.send(note, tokens).then(result => {
  21. console.log('sent:', result.sent.length);
  22. console.log('failed:', result.failed.length);
  23. console.log(result.failed);
  24. // For one-shot notification tasks you may wish to shutdown the connection
  25. // after everything is sent, but only call shutdown if you need your
  26. // application to terminate.
  27. service.shutdown();
  28. });