sending-multiple-notifications.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. Send individualised notifications
  3. i.e. Account updates for users with one-or-more device tokens
  4. */
  5. const apn = require('@parse/node-apn');
  6. const users = [
  7. { name: 'Wendy', devices: ['<insert device token>', '<insert device token>'] },
  8. { name: 'John', devices: ['<insert device token>'] },
  9. ];
  10. const service = new apn.Provider({
  11. cert: 'certificates/cert.pem',
  12. key: 'certificates/key.pem',
  13. });
  14. Promise.all(
  15. users.map(user => {
  16. const note = new apn.Notification();
  17. note.alert = `Hey ${user.name}, I just sent my first Push Notification`;
  18. // The topic is usually the bundle identifier of your application.
  19. note.topic = '<bundle identifier>';
  20. console.log(`Sending: ${note.compile()} to ${user.devices}`);
  21. return service.send(note, user.devices).then(result => {
  22. console.log('sent:', result.sent.length);
  23. console.log('failed:', result.failed.length);
  24. console.log(result.failed);
  25. });
  26. })
  27. ).then(() => {
  28. // For one-shot notification tasks you may wish to shutdown the connection
  29. // after everything is sent, but only call shutdown if you need your
  30. // application to terminate.
  31. service.shutdown();
  32. });