index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Create a notification
  3. * @constructor
  4. */
  5. function Notification(payload) {
  6. this.encoding = 'utf8';
  7. this.payload = {};
  8. this.compiled = false;
  9. this.aps = {};
  10. this.expiry = -1;
  11. this.priority = 10;
  12. if (payload) {
  13. /* TODO: consider using Object.entries in a separate change from introducing linting. */
  14. for (const key in payload) {
  15. if (Object.hasOwnProperty.call(payload, key)) {
  16. this[key] = payload[key];
  17. }
  18. }
  19. }
  20. }
  21. Notification.prototype = require('./apsProperties');
  22. // Create setter methods for properties
  23. [
  24. 'payload',
  25. 'expiry',
  26. 'priority',
  27. 'alert',
  28. 'body',
  29. 'locKey',
  30. 'locArgs',
  31. 'title',
  32. 'subtitle',
  33. 'titleLocKey',
  34. 'titleLocArgs',
  35. 'action',
  36. 'actionLocKey',
  37. 'launchImage',
  38. 'badge',
  39. 'sound',
  40. 'contentAvailable',
  41. 'mutableContent',
  42. 'mdm',
  43. 'urlArgs',
  44. 'category',
  45. 'threadId',
  46. 'interruptionLevel',
  47. 'targetContentIdentifier',
  48. 'relevanceScore',
  49. 'timestamp',
  50. 'staleDate',
  51. 'events',
  52. 'contentState',
  53. ].forEach(propName => {
  54. const methodName = 'set' + propName[0].toUpperCase() + propName.slice(1);
  55. Notification.prototype[methodName] = function (value) {
  56. this[propName] = value;
  57. return this;
  58. };
  59. });
  60. Notification.prototype.headers = function headers() {
  61. const headers = {};
  62. if (this.priority !== 10) {
  63. headers['apns-priority'] = this.priority;
  64. }
  65. if (this.id) {
  66. headers['apns-id'] = this.id;
  67. }
  68. if (this.expiry >= 0) {
  69. headers['apns-expiration'] = this.expiry;
  70. }
  71. if (this.topic) {
  72. headers['apns-topic'] = this.topic;
  73. }
  74. if (this.collapseId) {
  75. headers['apns-collapse-id'] = this.collapseId;
  76. }
  77. if (this.pushType) {
  78. headers['apns-push-type'] = this.pushType;
  79. }
  80. return headers;
  81. };
  82. /**
  83. * Compile a notification down to its JSON format. Compilation is final, changes made to the notification after this method is called will not be reflected in further calls.
  84. * @returns {String} JSON payload for the notification.
  85. * @since v1.3.0
  86. */
  87. Notification.prototype.compile = function () {
  88. if (!this.compiled) {
  89. this.compiled = JSON.stringify(this);
  90. }
  91. return this.compiled;
  92. };
  93. /**
  94. * @returns {Number} Byte length of the notification payload
  95. * @since v1.2.0
  96. */
  97. Notification.prototype.length = function () {
  98. return Buffer.byteLength(this.compile(), this.encoding || 'utf8');
  99. };
  100. /**
  101. * @private
  102. */
  103. Notification.prototype.apsPayload = function () {
  104. const { aps } = this;
  105. return Object.keys(aps).find(key => aps[key] !== undefined) ? aps : undefined;
  106. };
  107. Notification.prototype.toJSON = function () {
  108. if (this.rawPayload != null) {
  109. return this.rawPayload;
  110. }
  111. if (typeof this._mdm === 'string') {
  112. return { mdm: this._mdm };
  113. }
  114. return { ...this.payload, aps: this.apsPayload() };
  115. };
  116. module.exports = Notification;