Push.js 2.9 KB

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