Push.js 2.7 KB

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