local-notification-core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. var exec = require('cordova/exec');
  22. /**
  23. * Check permission to show notifications.
  24. *
  25. * @param [ Function ] callback The function to be exec as the callback.
  26. * @param [ Object ] scope The callback function's scope.
  27. *
  28. * @return [ Void ]
  29. */
  30. exports.hasPermission = function (callback, scope) {
  31. var fn = this.createCallbackFn(callback, scope);
  32. exec(fn, null, 'LocalNotification', 'check', []);
  33. };
  34. /**
  35. * Request permission to show notifications.
  36. *
  37. * @param [ Function ] callback The function to be exec as the callback.
  38. * @param [ Object ] scope The callback function's scope.
  39. *
  40. * @return [ Void ]
  41. */
  42. exports.requestPermission = function (callback, scope) {
  43. var fn = this.createCallbackFn(callback, scope);
  44. exec(fn, null, 'LocalNotification', 'request', []);
  45. };
  46. /**
  47. * Schedule notifications.
  48. *
  49. * @param [ Array ] notifications The notifications to schedule.
  50. * @param [ Function ] callback The function to be exec as the callback.
  51. * @param [ Object ] scope The callback function's scope.
  52. * @param [ Object ] args Optional flags how to schedule.
  53. *
  54. * @return [ Void ]
  55. */
  56. exports.schedule = function (msgs, callback, scope, args) {
  57. var fn = function (granted) {
  58. if (!granted) return;
  59. var toasts = this.toArray(msgs);
  60. for (var toast of toasts) {
  61. this.mergeWithDefaults(toast);
  62. this.convertProperties(toast);
  63. }
  64. this.exec('schedule', toasts, callback, scope);
  65. };
  66. if (args && args.skipPermission) {
  67. fn.call(this, true);
  68. } else {
  69. this.requestPermission(fn, this);
  70. }
  71. };
  72. /**
  73. * Schedule notifications.
  74. *
  75. * @param [ Array ] notifications The notifications to schedule.
  76. * @param [ Function ] callback The function to be exec as the callback.
  77. * @param [ Object ] scope The callback function's scope.
  78. * @param [ Object ] args Optional flags how to schedule.
  79. *
  80. * @return [ Void ]
  81. */
  82. exports.update = function (msgs, callback, scope, args) {
  83. var fn = function(granted) {
  84. if (!granted) return;
  85. var toasts = this.toArray(msgs);
  86. for (var toast of toasts) {
  87. this.convertProperties(toast); }
  88. this.exec('update', toasts, callback, scope);
  89. };
  90. if (args && args.skipPermission) {
  91. fn.call(this, true);
  92. } else {
  93. this.requestPermission(fn, this);
  94. }
  95. };
  96. /**
  97. * Clear the specified notifications by id.
  98. *
  99. * @param [ Array<Int> ] ids The IDs of the notifications.
  100. * @param [ Function ] callback The function to be exec as the callback.
  101. * @param [ Object ] scope The callback function's scope.
  102. *
  103. * @return [ Void ]
  104. */
  105. exports.clear = function (ids, callback, scope) {
  106. ids = this.toArray(ids);
  107. ids = this.convertIds(ids);
  108. this.exec('clear', ids, callback, scope);
  109. };
  110. /**
  111. * Clear all triggered notifications.
  112. *
  113. * @param [ Function ] callback The function to be exec as the callback.
  114. * @param [ Object ] scope The callback function's scope.
  115. *
  116. * @return [ Void ]
  117. */
  118. exports.clearAll = function (callback, scope) {
  119. this.exec('clearAll', null, callback, scope);
  120. };
  121. /**
  122. * Clear the specified notifications by id.
  123. *
  124. * @param [ Array<Int> ] ids The IDs of the notifications.
  125. * @param [ Function ] callback The function to be exec as the callback.
  126. * @param [ Object ] scope The callback function's scope.
  127. *
  128. * @return [ Void ]
  129. */
  130. exports.cancel = function (ids, callback, scope) {
  131. ids = this.toArray(ids);
  132. ids = this.convertIds(ids);
  133. this.exec('cancel', ids, callback, scope);
  134. };
  135. /**
  136. * Cancel all scheduled notifications.
  137. *
  138. * @param [ Function ] callback The function to be exec as the callback.
  139. * @param [ Object ] scope The callback function's scope.
  140. *
  141. * @return [ Void ]
  142. */
  143. exports.cancelAll = function (callback, scope) {
  144. this.exec('cancelAll', null, callback, scope);
  145. };
  146. /**
  147. * Check if a notification is present.
  148. *
  149. * @param [ Int ] id The ID of the notification.
  150. * @param [ Function ] callback The function to be exec as the callback.
  151. * @param [ Object ] scope The callback function's scope.
  152. *
  153. * @return [ Void ]
  154. */
  155. exports.isPresent = function (id, callback, scope) {
  156. var fn = this.createCallbackFn(callback, scope);
  157. this.getType(id, function (type) {
  158. fn(type != 'unknown');
  159. });
  160. };
  161. /**
  162. * Check if a notification has a given type.
  163. *
  164. * @param [ Int ] id The ID of the notification.
  165. * @param [ String ] type The type of the notification.
  166. * @param [ Function ] callback The function to be exec as the callback.
  167. * @param [ Object ] scope The callback function's scope.
  168. *
  169. * @return [ Void ]
  170. */
  171. exports.hasType = function (id, type, callback, scope) {
  172. var fn = this.createCallbackFn(callback, scope);
  173. this.getType(id, function (type2) {
  174. fn(type == type2);
  175. });
  176. };
  177. /**
  178. * Get the type (triggered, scheduled) for the notification.
  179. *
  180. * @param [ Int ] id The ID of the notification.
  181. * @param [ Function ] callback The function to be exec as the callback.
  182. * @param [ Object ] scope The callback function's scope.
  183. *
  184. * @return [ Void ]
  185. */
  186. exports.getType = function (id, callback, scope) {
  187. this.exec('type', id, callback, scope);
  188. };
  189. /**
  190. * List of all notification ids.
  191. *
  192. * @param [ Function ] callback The function to be exec as the callback.
  193. * @param [ Object ] scope The callback function's scope.
  194. *
  195. * @return [ Void ]
  196. */
  197. exports.getIds = function (callback, scope) {
  198. this.exec('ids', null, callback, scope);
  199. };
  200. /**
  201. * List of all scheduled notification IDs.
  202. *
  203. * @param [ Function ] callback The function to be exec as the callback.
  204. * @param [ Object ] scope The callback function's scope.
  205. *
  206. * @return [ Void ]
  207. */
  208. exports.getScheduledIds = function (callback, scope) {
  209. this.exec('scheduledIds', null, callback, scope);
  210. };
  211. /**
  212. * List of all triggered notification IDs.
  213. *
  214. * @param [ Function ] callback The function to be exec as the callback.
  215. * @param [ Object ] scope The callback function's scope.
  216. *
  217. * @return [ Void ]
  218. */
  219. exports.getTriggeredIds = function (callback, scope) {
  220. this.exec('triggeredIds', null, callback, scope);
  221. };
  222. /**
  223. * List of local notifications specified by id.
  224. * If called without IDs, all notification will be returned.
  225. *
  226. * @param [ Array<Int> ] ids The IDs of the notifications.
  227. * @param [ Function ] callback The function to be exec as the callback.
  228. * @param [ Object ] scope The callback function's scope.
  229. *
  230. * @return [ Void ]
  231. */
  232. exports.get = function () {
  233. var args = Array.apply(null, arguments);
  234. if (typeof args[0] == 'function') {
  235. args.unshift([]);
  236. }
  237. var ids = args[0],
  238. callback = args[1],
  239. scope = args[2];
  240. if (!Array.isArray(ids)) {
  241. this.exec('notification', Number(ids), callback, scope);
  242. return;
  243. }
  244. ids = this.convertIds(ids);
  245. this.exec('notifications', ids, callback, scope);
  246. };
  247. /**
  248. * List for all notifications.
  249. *
  250. * @param [ Function ] callback The function to be exec as the callback.
  251. * @param [ Object ] scope The callback function's scope.
  252. *
  253. * @return [ Void ]
  254. */
  255. exports.getAll = function (callback, scope) {
  256. this.exec('notifications', null, callback, scope);
  257. };
  258. /**
  259. * List of all scheduled notifications.
  260. *
  261. * @param [ Function ] callback The function to be exec as the callback.
  262. * @param [ Object ] scope The callback function's scope.
  263. */
  264. exports.getScheduled = function (callback, scope) {
  265. this.exec('scheduledNotifications', null, callback, scope);
  266. };
  267. /**
  268. * List of all triggered notifications.
  269. *
  270. * @param [ Function ] callback The function to be exec as the callback.
  271. * @param [ Object ] scope The callback function's scope.
  272. */
  273. exports.getTriggered = function (callback, scope) {
  274. this.exec('triggeredNotifications', null, callback, scope);
  275. };
  276. /**
  277. * Register an group of actions by id.
  278. *
  279. * @param [ String ] id The Id of the group.
  280. * @param [ Array] actions The action config settings.
  281. * @param [ Function ] callback The function to be exec as the callback.
  282. * @param [ Object ] scope The callback function's scope.
  283. *
  284. * @return [ Void ]
  285. */
  286. exports.addActionGroup = function (id, actions, callback, scope) {
  287. var config = { actionGroupId: id, actions: actions };
  288. this.exec('actions', config, callback, scope);
  289. };
  290. /**
  291. * The (platform specific) default settings.
  292. *
  293. * @return [ Object ]
  294. */
  295. exports.getDefaults = function () {
  296. var map = Object.create(this._defaults);
  297. for (var key in map) {
  298. if (Array.isArray(map[key])) {
  299. map[key] = Array.from(map[key]);
  300. } else
  301. if (Object.prototype.isPrototypeOf(map[key])) {
  302. map[key] = Object.create(map[key]);
  303. }
  304. }
  305. return map;
  306. };
  307. /**
  308. * Overwrite default settings.
  309. *
  310. * @param [ Object ] newDefaults New default values.
  311. *
  312. * @return [ Void ]
  313. */
  314. exports.setDefaults = function (newDefaults) {
  315. Object.assign(this._defaults, newDefaults);
  316. };
  317. /**
  318. * Register callback for given event.
  319. *
  320. * @param [ String ] event The name of the event.
  321. * @param [ Function ] callback The function to be exec as callback.
  322. * @param [ Object ] scope The callback function's scope.
  323. *
  324. * @return [ Void ]
  325. */
  326. exports.on = function (event, callback, scope) {
  327. if (typeof callback !== "function")
  328. return;
  329. if (!this._listener[event]) {
  330. this._listener[event] = [];
  331. }
  332. var item = [callback, scope || window];
  333. this._listener[event].push(item);
  334. };
  335. /**
  336. * Unregister callback for given event.
  337. *
  338. * @param [ String ] event The name of the event.
  339. * @param [ Function ] callback The function to be exec as callback.
  340. *
  341. * @return [ Void ]
  342. */
  343. exports.un = function (event, callback) {
  344. var listener = this._listener[event];
  345. if (!listener)
  346. return;
  347. for (var i = 0; i < listener.length; i++) {
  348. var fn = listener[i][0];
  349. if (fn == callback) {
  350. listener.splice(i, 1);
  351. break;
  352. }
  353. }
  354. };