local-notification-core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. var toasts = this.toArray(msgs);
  59. if (!granted && callback) {
  60. callback.call(scope || this, false);
  61. return;
  62. }
  63. for (var i = 0, len = toasts.length; i < len; i++) {
  64. var toast = toasts[i];
  65. this.mergeWithDefaults(toast);
  66. this.convertProperties(toast);
  67. }
  68. this.exec('schedule', toasts, callback, scope);
  69. };
  70. if (args && args.skipPermission) {
  71. fn.call(this, true);
  72. } else {
  73. this.requestPermission(fn, this);
  74. }
  75. };
  76. /**
  77. * Schedule notifications.
  78. *
  79. * @param [ Array ] notifications The notifications to schedule.
  80. * @param [ Function ] callback The function to be exec as the callback.
  81. * @param [ Object ] scope The callback function's scope.
  82. * @param [ Object ] args Optional flags how to schedule.
  83. *
  84. * @return [ Void ]
  85. */
  86. exports.update = function (msgs, callback, scope, args) {
  87. var fn = function(granted) {
  88. var toasts = this.toArray(msgs);
  89. if (!granted && callback) {
  90. callback.call(scope || this, false);
  91. return;
  92. }
  93. for (var i = 0, len = toasts.length; i < len; i++) {
  94. this.convertProperties(toasts[i]);
  95. }
  96. this.exec('update', toasts, callback, scope);
  97. };
  98. if (args && args.skipPermission) {
  99. fn.call(this, true);
  100. } else {
  101. this.requestPermission(fn, this);
  102. }
  103. };
  104. /**
  105. * Clear the specified notifications by id.
  106. *
  107. * @param [ Array<Int> ] ids The IDs of the notifications.
  108. * @param [ Function ] callback The function to be exec as the callback.
  109. * @param [ Object ] scope The callback function's scope.
  110. *
  111. * @return [ Void ]
  112. */
  113. exports.clear = function (ids, callback, scope) {
  114. ids = this.toArray(ids);
  115. ids = this.convertIds(ids);
  116. this.exec('clear', ids, callback, scope);
  117. };
  118. /**
  119. * Clear all triggered notifications.
  120. *
  121. * @param [ Function ] callback The function to be exec as the callback.
  122. * @param [ Object ] scope The callback function's scope.
  123. *
  124. * @return [ Void ]
  125. */
  126. exports.clearAll = function (callback, scope) {
  127. this.exec('clearAll', null, callback, scope);
  128. };
  129. /**
  130. * Clear the specified notifications by id.
  131. *
  132. * @param [ Array<Int> ] ids The IDs of the notifications.
  133. * @param [ Function ] callback The function to be exec as the callback.
  134. * @param [ Object ] scope The callback function's scope.
  135. *
  136. * @return [ Void ]
  137. */
  138. exports.cancel = function (ids, callback, scope) {
  139. ids = this.toArray(ids);
  140. ids = this.convertIds(ids);
  141. this.exec('cancel', ids, callback, scope);
  142. };
  143. /**
  144. * Cancel all scheduled notifications.
  145. *
  146. * @param [ Function ] callback The function to be exec as the callback.
  147. * @param [ Object ] scope The callback function's scope.
  148. *
  149. * @return [ Void ]
  150. */
  151. exports.cancelAll = function (callback, scope) {
  152. this.exec('cancelAll', null, callback, scope);
  153. };
  154. /**
  155. * Check if a notification is present.
  156. *
  157. * @param [ Int ] id The ID of the notification.
  158. * @param [ Function ] callback The function to be exec as the callback.
  159. * @param [ Object ] scope The callback function's scope.
  160. *
  161. * @return [ Void ]
  162. */
  163. exports.isPresent = function (id, callback, scope) {
  164. var fn = this.createCallbackFn(callback, scope);
  165. this.getType(id, function (type) {
  166. fn(type != 'unknown');
  167. });
  168. };
  169. /**
  170. * Check if a notification has a given type.
  171. *
  172. * @param [ Int ] id The ID of the notification.
  173. * @param [ String ] type The type of the notification.
  174. * @param [ Function ] callback The function to be exec as the callback.
  175. * @param [ Object ] scope The callback function's scope.
  176. *
  177. * @return [ Void ]
  178. */
  179. exports.hasType = function (id, type, callback, scope) {
  180. var fn = this.createCallbackFn(callback, scope);
  181. this.getType(id, function (type2) {
  182. fn(type == type2);
  183. });
  184. };
  185. /**
  186. * Get the type (triggered, scheduled) for the notification.
  187. *
  188. * @param [ Int ] id The ID of the notification.
  189. * @param [ Function ] callback The function to be exec as the callback.
  190. * @param [ Object ] scope The callback function's scope.
  191. *
  192. * @return [ Void ]
  193. */
  194. exports.getType = function (id, callback, scope) {
  195. this.exec('type', id, callback, scope);
  196. };
  197. /**
  198. * List of all notification ids.
  199. *
  200. * @param [ Function ] callback The function to be exec as the callback.
  201. * @param [ Object ] scope The callback function's scope.
  202. *
  203. * @return [ Void ]
  204. */
  205. exports.getIds = function (callback, scope) {
  206. this.exec('ids', null, callback, scope);
  207. };
  208. /**
  209. * List of all scheduled notification IDs.
  210. *
  211. * @param [ Function ] callback The function to be exec as the callback.
  212. * @param [ Object ] scope The callback function's scope.
  213. *
  214. * @return [ Void ]
  215. */
  216. exports.getScheduledIds = function (callback, scope) {
  217. this.exec('scheduledIds', null, callback, scope);
  218. };
  219. /**
  220. * List of all triggered notification IDs.
  221. *
  222. * @param [ Function ] callback The function to be exec as the callback.
  223. * @param [ Object ] scope The callback function's scope.
  224. *
  225. * @return [ Void ]
  226. */
  227. exports.getTriggeredIds = function (callback, scope) {
  228. this.exec('triggeredIds', null, callback, scope);
  229. };
  230. /**
  231. * List of local notifications specified by id.
  232. * If called without IDs, all notification will be returned.
  233. *
  234. * @param [ Array<Int> ] ids The IDs of the notifications.
  235. * @param [ Function ] callback The function to be exec as the callback.
  236. * @param [ Object ] scope The callback function's scope.
  237. *
  238. * @return [ Void ]
  239. */
  240. exports.get = function () {
  241. var args = Array.apply(null, arguments);
  242. if (typeof args[0] == 'function') {
  243. args.unshift([]);
  244. }
  245. var ids = args[0],
  246. callback = args[1],
  247. scope = args[2];
  248. if (!Array.isArray(ids)) {
  249. this.exec('notification', Number(ids), callback, scope);
  250. return;
  251. }
  252. ids = this.convertIds(ids);
  253. this.exec('notifications', ids, callback, scope);
  254. };
  255. /**
  256. * List for all notifications.
  257. *
  258. * @param [ Function ] callback The function to be exec as the callback.
  259. * @param [ Object ] scope The callback function's scope.
  260. *
  261. * @return [ Void ]
  262. */
  263. exports.getAll = function (callback, scope) {
  264. this.exec('notifications', null, callback, scope);
  265. };
  266. /**
  267. * List of all scheduled notifications.
  268. *
  269. * @param [ Function ] callback The function to be exec as the callback.
  270. * @param [ Object ] scope The callback function's scope.
  271. */
  272. exports.getScheduled = function (callback, scope) {
  273. this.exec('scheduledNotifications', null, callback, scope);
  274. };
  275. /**
  276. * List of all triggered notifications.
  277. *
  278. * @param [ Function ] callback The function to be exec as the callback.
  279. * @param [ Object ] scope The callback function's scope.
  280. */
  281. exports.getTriggered = function (callback, scope) {
  282. this.exec('triggeredNotifications', null, callback, scope);
  283. };
  284. /**
  285. * Register an group of actions by id.
  286. *
  287. * @param [ String ] id The Id of the group.
  288. * @param [ Array] actions The action config settings.
  289. * @param [ Function ] callback The function to be exec as the callback.
  290. * @param [ Object ] scope The callback function's scope.
  291. *
  292. * @return [ Void ]
  293. */
  294. exports.addActionGroup = function (id, actions, callback, scope) {
  295. var config = { actionGroupId: id, actions: actions };
  296. this.exec('actions', config, callback, scope);
  297. };
  298. /**
  299. * The (platform specific) default settings.
  300. *
  301. * @return [ Object ]
  302. */
  303. exports.getDefaults = function () {
  304. var map = Object.assign({}, this._defaults);
  305. for (var key in map) {
  306. if (Array.isArray(map[key])) {
  307. map[key] = Array.from(map[key]);
  308. } else
  309. if (Object.prototype.isPrototypeOf(map[key])) {
  310. map[key] = Object.assign({}, map[key]);
  311. }
  312. }
  313. return map;
  314. };
  315. /**
  316. * Overwrite default settings.
  317. *
  318. * @param [ Object ] newDefaults New default values.
  319. *
  320. * @return [ Void ]
  321. */
  322. exports.setDefaults = function (newDefaults) {
  323. Object.assign(this._defaults, newDefaults);
  324. };
  325. /**
  326. * Register callback for given event.
  327. *
  328. * @param [ String ] event The name of the event.
  329. * @param [ Function ] callback The function to be exec as callback.
  330. * @param [ Object ] scope The callback function's scope.
  331. *
  332. * @return [ Void ]
  333. */
  334. exports.on = function (event, callback, scope) {
  335. if (typeof callback !== "function")
  336. return;
  337. if (!this._listener[event]) {
  338. this._listener[event] = [];
  339. }
  340. var item = [callback, scope || window];
  341. this._listener[event].push(item);
  342. };
  343. /**
  344. * Unregister callback for given event.
  345. *
  346. * @param [ String ] event The name of the event.
  347. * @param [ Function ] callback The function to be exec as callback.
  348. *
  349. * @return [ Void ]
  350. */
  351. exports.un = function (event, callback) {
  352. var listener = this._listener[event];
  353. if (!listener)
  354. return;
  355. for (var i = 0; i < listener.length; i++) {
  356. var fn = listener[i][0];
  357. if (fn == callback) {
  358. listener.splice(i, 1);
  359. break;
  360. }
  361. }
  362. };