pushjs.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (C) 2019 Silvio Clecio (silvioprog) and
  4. Fernando Baroni (frbaroni).
  5. Pascal mapping for PushJS: https://pushjs.org
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { Compact and cross-browser solution for Notifications API. }
  13. {$IFNDEF FPC_DOTTEDUNITS}
  14. unit PushJS;
  15. {$ENDIF}
  16. {$MODE OBJFPC}
  17. {$MODESWITCH EXTERNALCLASS}
  18. interface
  19. uses
  20. {$IFDEF FPC_DOTTEDUNITS}
  21. JSApi.JS;
  22. {$ELSE}
  23. JS;
  24. {$ENDIF}
  25. { TODO:
  26. - Plugins: https://pushjs.org/docs/plugins }
  27. type
  28. { TPushFunction }
  29. TPushFunction = reference to procedure;
  30. { TPushOptions }
  31. TPushOptions = class external name 'Object'
  32. // The body text of the notification
  33. body: string;
  34. // Data to pass to ServiceWorker notifications
  35. data: JSValue;
  36. // Can be either the URL to an icon image or an array containing 16x16 and 32x32 pixel icon images (see above).
  37. icon: string;
  38. // A relative URL path to navigate to when the user clicks on the notification on mobile (e.g. if you want users
  39. // to navigate to your page http://example.com/page, then the relative URL is just page). If the page is already
  40. // open in the background, then the browser window will automatically become focused. Requires the serviceWorker.js
  41. // file to be present on your server to work.
  42. link: string;
  43. // When set to true, the notification will not close unless the user manually closes or clicks on it
  44. requireInteraction: Boolean;
  45. // Unique tag used to identify the notification. Can be used to later close the notification manually.
  46. tag: string;
  47. // Time in milliseconds until the notification closes automatically
  48. timeout: Integer;
  49. // An array of durations for a mobile device receiving the notification to vibrate. For example, [200, 100] would
  50. // vibrate first for 200 milliseconds, pause, then continue for 100 milliseconds. For more information, see below.
  51. // Available in Mobile Chrome only.
  52. vibrate: Boolean;
  53. // Specifies whether the notification should be silent, i.e. no sounds or vibrations should be issued, regardless
  54. // of the device settings.
  55. // Supported only in Chrome 43.0 or higher.
  56. silent: Boolean;
  57. // Callback to execute when the notification is clicked
  58. onClick: TPushFunction;
  59. // Callback to execute when if the notification throws an error
  60. onError: TPushFunction;
  61. // Creates new push options
  62. constructor new;
  63. end;
  64. { TPushFallbackPayload }
  65. TPushFallbackPayload = class external name 'Object'
  66. // Notification title
  67. title: string;
  68. // Notification body
  69. body: string;
  70. // Notification tag
  71. tag: string;
  72. // Notification icon
  73. icon: string;
  74. end;
  75. { TPushParamsFallback }
  76. TPushParamsFallback = reference to procedure(payload: TPushFallbackPayload);
  77. { TPushParams }
  78. TPushParams = class external name 'Object'
  79. // Sets a custom service worker script
  80. serviceWorker: string;
  81. // Code that executes on browsers with no notification support
  82. // "payload" is an object containing the
  83. // title, body, tag, and icon of the notification
  84. fallback: TPushParamsFallback;
  85. end;
  86. { TPushPermission }
  87. TPushPermission = class external name 'Object'
  88. private
  89. FDEFAULT: string; external name 'DEFAULT';
  90. FGRANTED: string; external name 'GRANTED';
  91. FDENIED: string; external name 'DENIED';
  92. public
  93. // Requests permission for desktop notifications
  94. procedure request(
  95. // Function to execute once permission is granted
  96. onGranted: TPushFunction;
  97. // Function to execute once permission is denied
  98. onDenied: TPushFunction);
  99. // Returns whether Push has been granted permission to run
  100. function has: Boolean;
  101. // Gets the permission level
  102. function get: string;
  103. // 'default'
  104. property DEFAULT: string read FDEFAULT;
  105. // 'granted'
  106. property GRANTED: string read FGRANTED;
  107. // 'denied'
  108. property DENIED: string read FDENIED;
  109. end;
  110. { TPush }
  111. TPush = class external name 'Push' (TJSPromise)
  112. private class var
  113. FPermission: TPushPermission; external name 'Permission';
  114. public
  115. // Creates and displays a new notification
  116. class function create(
  117. // Notification title
  118. const title: string): TPush; overload; external name 'create';
  119. // Creates and displays a new notification
  120. class function create(
  121. // Notification title
  122. const title: string;
  123. // Notification options
  124. params: TPushOptions): TPush; overload; external name 'create';
  125. // Returns the notification count
  126. class function count: Integer;
  127. // Closes a notification with the given tag
  128. // Return boolean denoting success
  129. class function close(
  130. // Tag of the notification to close
  131. const tag: string): Boolean;
  132. // Clears all notifications
  133. // Return boolean denoting whether the clear was successful in closing all notifications
  134. class function clear: Boolean;
  135. // Denotes whether Push is supported in the current browser
  136. class function supported: Boolean;
  137. // Modifies settings or returns all settings if no parameter passed
  138. class procedure config(params: TPushParams);
  139. // Permission object
  140. class property Permission: TPushPermission read FPermission;
  141. end;
  142. implementation
  143. end.