Push.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * @class Parse.Push
  26. * @static
  27. * @hideconstructor
  28. */
  29. /**
  30. * Sends a push notification.
  31. * @method send
  32. * @name Parse.Push.send
  33. * @param {Object} data - The data of the push notification. Valid fields
  34. * are:
  35. * <ol>
  36. * <li>channels - An Array of channels to push to.</li>
  37. * <li>push_time - A Date object for when to send the push.</li>
  38. * <li>expiration_time - A Date object for when to expire
  39. * the push.</li>
  40. * <li>expiration_interval - The seconds from now to expire the push.</li>
  41. * <li>where - A Parse.Query over Parse.Installation that is used to match
  42. * a set of installations to push to.</li>
  43. * <li>data - The data to send as part of the push</li>
  44. * <ol>
  45. * @param {Object} options An object that has an optional success function,
  46. * that takes no arguments and will be called on a successful push, and
  47. * an error function that takes a Parse.Error and will be called if the push
  48. * failed.
  49. * @return {Promise} A promise that is fulfilled when the push request
  50. * completes.
  51. */
  52. function send(data
  53. /*: PushData*/
  54. , options
  55. /*:: ?: { useMasterKey?: boolean, success?: any, error?: any }*/
  56. )
  57. /*: Promise*/
  58. {
  59. options = options || {};
  60. if (data.where && data.where instanceof _ParseQuery.default) {
  61. data.where = data.where.toJSON().where;
  62. }
  63. if (data.push_time && typeof data.push_time === 'object') {
  64. data.push_time = data.push_time.toJSON();
  65. }
  66. if (data.expiration_time && typeof data.expiration_time === 'object') {
  67. data.expiration_time = data.expiration_time.toJSON();
  68. }
  69. if (data.expiration_time && data.expiration_interval) {
  70. throw new Error('expiration_time and expiration_interval cannot both be set.');
  71. }
  72. return _CoreManager.default.getPushController().send(data, {
  73. useMasterKey: options.useMasterKey
  74. });
  75. }
  76. const DefaultController = {
  77. send(data
  78. /*: PushData*/
  79. , options
  80. /*: RequestOptions*/
  81. ) {
  82. const RESTController = _CoreManager.default.getRESTController();
  83. const request = RESTController.request('POST', 'push', data, {
  84. useMasterKey: !!options.useMasterKey
  85. });
  86. return request;
  87. }
  88. };
  89. _CoreManager.default.setPushController(DefaultController);