Push.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.send = send;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {
  10. default: obj
  11. };
  12. }
  13. /**
  14. * Copyright (c) 2015-present, Parse, LLC.
  15. * All rights reserved.
  16. *
  17. * This source code is licensed under the BSD-style license found in the
  18. * LICENSE file in the root directory of this source tree. An additional grant
  19. * of patent rights can be found in the PATENTS file in the same directory.
  20. *
  21. * @flow
  22. */
  23. /**
  24. * Contains functions to deal with Push in Parse.
  25. *
  26. * @class Parse.Push
  27. * @static
  28. * @hideconstructor
  29. */
  30. /**
  31. * Sends a push notification.
  32. * **Available in Cloud Code only.**
  33. *
  34. * See {@link https://docs.parseplatform.org/js/guide/#push-notifications Push Notification Guide}
  35. *
  36. * @function send
  37. * @name Parse.Push.send
  38. * @param {object} data - The data of the push notification. Valid fields
  39. * are:
  40. * <ol>
  41. * <li>channels - An Array of channels to push to.</li>
  42. * <li>push_time - A Date object for when to send the push.</li>
  43. * <li>expiration_time - A Date object for when to expire
  44. * the push.</li>
  45. * <li>expiration_interval - The seconds from now to expire the push.</li>
  46. * <li>where - A Parse.Query over Parse.Installation that is used to match
  47. * a set of installations to push to.</li>
  48. * <li>data - The data to send as part of the push.</li>
  49. * <ol>
  50. * @returns {Promise} A promise that is fulfilled when the push request
  51. * completes.
  52. */
  53. function send(data
  54. /*: PushData*/
  55. )
  56. /*: Promise*/
  57. {
  58. if (data.where && data.where instanceof _ParseQuery.default) {
  59. data.where = data.where.toJSON().where;
  60. }
  61. if (data.push_time && typeof data.push_time === 'object') {
  62. data.push_time = data.push_time.toJSON();
  63. }
  64. if (data.expiration_time && typeof data.expiration_time === 'object') {
  65. data.expiration_time = data.expiration_time.toJSON();
  66. }
  67. if (data.expiration_time && data.expiration_interval) {
  68. throw new Error('expiration_time and expiration_interval cannot both be set.');
  69. }
  70. return _CoreManager.default.getPushController().send(data);
  71. }
  72. const DefaultController = {
  73. send(data
  74. /*: PushData*/
  75. ) {
  76. return _CoreManager.default.getRESTController().request('POST', 'push', data, {
  77. useMasterKey: true
  78. });
  79. }
  80. };
  81. _CoreManager.default.setPushController(DefaultController);