Push.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPushStatus = getPushStatus;
  6. exports.send = send;
  7. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  8. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. /**
  15. * Copyright (c) 2015-present, Parse, LLC.
  16. * All rights reserved.
  17. *
  18. * This source code is licensed under the BSD-style license found in the
  19. * LICENSE file in the root directory of this source tree. An additional grant
  20. * of patent rights can be found in the PATENTS file in the same directory.
  21. *
  22. * @flow
  23. */
  24. /**
  25. * Contains functions to deal with Push in Parse.
  26. *
  27. * @class Parse.Push
  28. * @static
  29. * @hideconstructor
  30. */
  31. /**
  32. * Sends a push notification.
  33. * **Available in Cloud Code only.**
  34. *
  35. * See {@link https://docs.parseplatform.org/js/guide/#push-notifications Push Notification Guide}
  36. *
  37. * @function send
  38. * @name Parse.Push.send
  39. * @param {object} data - The data of the push notification. Valid fields
  40. * are:
  41. * <ol>
  42. * <li>channels - An Array of channels to push to.</li>
  43. * <li>push_time - A Date object for when to send the push.</li>
  44. * <li>expiration_time - A Date object for when to expire
  45. * the push.</li>
  46. * <li>expiration_interval - The seconds from now to expire the push.</li>
  47. * <li>where - A Parse.Query over Parse.Installation that is used to match
  48. * a set of installations to push to.</li>
  49. * <li>data - The data to send as part of the push.</li>
  50. * <ol>
  51. * @param {object} options Valid options
  52. * are:<ul>
  53. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  54. * be used for this request.
  55. * </ul>
  56. * @returns {Promise} A promise that is fulfilled when the push request
  57. * completes.
  58. */
  59. function send(data
  60. /*: PushData*/
  61. , options
  62. /*:: ?: FullOptions*/
  63. = {})
  64. /*: Promise*/
  65. {
  66. if (data.where && data.where instanceof _ParseQuery.default) {
  67. data.where = data.where.toJSON().where;
  68. }
  69. if (data.push_time && typeof data.push_time === 'object') {
  70. data.push_time = data.push_time.toJSON();
  71. }
  72. if (data.expiration_time && typeof data.expiration_time === 'object') {
  73. data.expiration_time = data.expiration_time.toJSON();
  74. }
  75. if (data.expiration_time && data.expiration_interval) {
  76. throw new Error('expiration_time and expiration_interval cannot both be set.');
  77. }
  78. const pushOptions = {
  79. useMasterKey: true
  80. };
  81. if (options.hasOwnProperty('useMasterKey')) {
  82. pushOptions.useMasterKey = options.useMasterKey;
  83. }
  84. return _CoreManager.default.getPushController().send(data, pushOptions);
  85. }
  86. /**
  87. * Gets push status by Id
  88. *
  89. * @function getPushStatus
  90. * @name Parse.Push.getPushStatus
  91. * @param {string} pushStatusId The Id of Push Status.
  92. * @param {object} options Valid options
  93. * are:<ul>
  94. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  95. * be used for this request.
  96. * </ul>
  97. * @returns {Parse.Object} Status of Push.
  98. */
  99. function getPushStatus(pushStatusId
  100. /*: string*/
  101. , options
  102. /*:: ?: FullOptions*/
  103. = {})
  104. /*: Promise<string>*/
  105. {
  106. const pushOptions = {
  107. useMasterKey: true
  108. };
  109. if (options.hasOwnProperty('useMasterKey')) {
  110. pushOptions.useMasterKey = options.useMasterKey;
  111. }
  112. const query = new _ParseQuery.default('_PushStatus');
  113. return query.get(pushStatusId, pushOptions);
  114. }
  115. const DefaultController = {
  116. send(data
  117. /*: PushData*/
  118. , options
  119. /*:: ?: FullOptions*/
  120. ) {
  121. return _CoreManager.default.getRESTController().request('POST', 'push', data, options);
  122. }
  123. };
  124. _CoreManager.default.setPushController(DefaultController);