Push.js 3.0 KB

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