LocalNotificationProxy.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 LocalNotification = LocalNotificationProxy.LocalNotification,
  22. ActivationKind = Windows.ApplicationModel.Activation.ActivationKind;
  23. var impl = new LocalNotificationProxy.LocalNotificationProxy();
  24. /**
  25. * Check permission to show notifications.
  26. *
  27. * @param [ Function ] success Success callback
  28. * @param [ Function ] error Error callback
  29. *
  30. * @return [ Void ]
  31. */
  32. exports.check = function (success, error) {
  33. var granted = impl.hasPermission();
  34. success(granted);
  35. };
  36. /**
  37. * Request permission to show notifications.
  38. *
  39. * @param [ Function ] success Success callback
  40. * @param [ Function ] error Error callback
  41. *
  42. * @return [ Void ]
  43. */
  44. exports.request = function (success, error) {
  45. exports.check(success, error);
  46. };
  47. /**
  48. * Schedule notifications.
  49. *
  50. * @param [ Function ] success Success callback
  51. * @param [ Function ] error Error callback
  52. * @param [ Array ] args Interface arguments
  53. *
  54. * @return [ Void ]
  55. */
  56. exports.schedule = function (success, error, args) {
  57. var options = [];
  58. for (var i = 0, props, opts; i < args.length; i++) {
  59. props = args[i];
  60. opts = exports.parseOptions(props);
  61. options.push(opts);
  62. }
  63. impl.schedule(options);
  64. for (i = 0; i < options.length; i++) {
  65. exports.fireEvent('add', options[i]);
  66. }
  67. success();
  68. };
  69. /**
  70. * Update notifications.
  71. *
  72. * @param [ Function ] success Success callback
  73. * @param [ Function ] error Error callback
  74. * @param [ Array ] args Interface arguments
  75. *
  76. * @return [ Void ]
  77. */
  78. exports.schedule = function (success, error, args) {
  79. console.warn('LocalNotification#update is not implemented.');
  80. success();
  81. };
  82. /**
  83. * Clear the notifications specified by id.
  84. *
  85. * @param [ Function ] success Success callback
  86. * @param [ Function ] error Error callback
  87. * @param [ Array ] args Interface arguments
  88. *
  89. * @return [ Void ]
  90. */
  91. exports.clear = function (success, error, args) {
  92. var toasts = impl.clear(args) || [];
  93. for (var i = 0; i < toasts.length; i++) {
  94. exports.fireEvent('clear', toasts[i]);
  95. }
  96. success();
  97. };
  98. /**
  99. * Clear all notifications.
  100. *
  101. * @param [ Function ] success Success callback
  102. * @param [ Function ] error Error callback
  103. *
  104. * @return [ Void ]
  105. */
  106. exports.clearAll = function (success, error) {
  107. impl.clearAll();
  108. exports.fireEvent('clearall');
  109. success();
  110. };
  111. /**
  112. * Cancel the notifications specified by id.
  113. *
  114. * @param [ Function ] success Success callback
  115. * @param [ Function ] error Error callback
  116. * @param [ Array ] args Interface arguments
  117. *
  118. * @return [ Void ]
  119. */
  120. exports.cancel = function (success, error, args) {
  121. var toasts = impl.cancel(args) || [];
  122. for (var i = 0; i < toasts.length; i++) {
  123. exports.fireEvent('cancel', toasts[i]);
  124. }
  125. success();
  126. };
  127. /**
  128. * Cancel all notifications.
  129. *
  130. * @param [ Function ] success Success callback
  131. * @param [ Function ] error Error callback
  132. *
  133. * @return [ Void ]
  134. */
  135. exports.cancelAll = function (success, error) {
  136. impl.cancelAll();
  137. exports.fireEvent('cancelall');
  138. success();
  139. };
  140. /**
  141. * Get the type of notification.
  142. *
  143. * @param [ Function ] success Success callback
  144. * @param [ Function ] error Error callback
  145. * @param [ Array ] args Interface arguments
  146. *
  147. * @return [ Void ]
  148. */
  149. exports.type = function (success, error, args) {
  150. var type = impl.type(args[0]);
  151. success(type);
  152. };
  153. /**
  154. * List of all notification ids.
  155. *
  156. * @param [ Function ] success Success callback
  157. * @param [ Function ] error Error callback
  158. *
  159. * @return [ Void ]
  160. */
  161. exports.ids = function (success, error) {
  162. var ids = impl.ids() || [];
  163. success(Array.from(ids));
  164. };
  165. /**
  166. * List of all scheduled notification ids.
  167. *
  168. * @param [ Function ] success Success callback
  169. * @param [ Function ] error Error callback
  170. *
  171. * @return [ Void ]
  172. */
  173. exports.scheduledIds = function (success, error) {
  174. var ids = impl.scheduledIds() || [];
  175. success(Array.from(ids));
  176. };
  177. /**
  178. * List of all triggered notification ids.
  179. *
  180. * @param [ Function ] success Success callback
  181. * @param [ Function ] error Error callback
  182. *
  183. * @return [ Void ]
  184. */
  185. exports.triggeredIds = function (success, error) {
  186. var ids = impl.triggeredIds() || [];
  187. success(Array.from(ids));
  188. };
  189. /**
  190. * Get a single notification by id.
  191. *
  192. * @param [ Function ] success Success callback
  193. * @param [ Function ] error Error callback
  194. * @param [ Array ] args Interface arguments
  195. *
  196. * @return [ Void ]
  197. */
  198. exports.notification = function (success, error, args) {
  199. var obj = impl.notification(args[0]);
  200. success(exports.clone(obj));
  201. };
  202. /**
  203. * List of (all) notifications.
  204. *
  205. * @param [ Function ] success Success callback
  206. * @param [ Function ] error Error callback
  207. * @param [ Array ] args Interface arguments
  208. *
  209. * @return [ Void ]
  210. */
  211. exports.notifications = function (success, error, args) {
  212. var objs = impl.notifications(args) || [];
  213. success(exports.cloneAll(objs));
  214. };
  215. /**
  216. * List of all scheduled notifications.
  217. *
  218. * @param [ Function ] success Success callback
  219. * @param [ Function ] error Error callback
  220. *
  221. * @return [ Void ]
  222. */
  223. exports.scheduledNotifications = function (success, error) {
  224. var objs = impl.scheduledNotifications() || [];
  225. success(exports.cloneAll(objs));
  226. };
  227. /**
  228. * List of all triggered notifications.
  229. *
  230. * @param [ Function ] success Success callback
  231. * @param [ Function ] error Error callback
  232. *
  233. * @return [ Void ]
  234. */
  235. exports.triggeredNotifications = function (success, error) {
  236. var objs = impl.triggeredNotifications() || [];
  237. success(exports.cloneAll(objs));
  238. };
  239. /**
  240. * Inform the user through the click event that a notification was clicked.
  241. *
  242. * @param [ String ] xml The launch identifier.
  243. *
  244. * @return [ Void ]
  245. */
  246. exports.clicked = function (xml, input) {
  247. var toast = LocalNotification.Options.parse(xml),
  248. event = toast.action || 'click',
  249. meta = Object.assign({}, input);
  250. if (input && input.size > 0) {
  251. meta.text = input.first().current.value;
  252. }
  253. exports.fireEvent(event, toast, meta);
  254. };
  255. /**
  256. * Invoke listeners for the given event.
  257. *
  258. * @param [ String ] event The name of the event.
  259. * @param [ Object ] toast Optional notification object.
  260. * @param [ Object ] data Optional meta data about the event.
  261. *
  262. * @return [ Void ]
  263. */
  264. exports.fireEvent = function (event, toast, data) {
  265. var meta = Object.assign({ event: event }, data),
  266. plugin = cordova.plugins.notification.local.core;
  267. if (toast) {
  268. plugin.fireEvent(event, exports.clone(toast), meta);
  269. } else {
  270. plugin.fireEvent(event, meta);
  271. }
  272. };
  273. /**
  274. * Clone the objects and delete internal properties.
  275. *
  276. * @param [ Array<Object> ] objs The objects to clone for.
  277. *
  278. * @return [ Array<Object> ]
  279. */
  280. exports.cloneAll = function (objs) {
  281. var clones = [];
  282. for (var i = 0; i < objs.length; i++) {
  283. clones.push(exports.clone(objs[i]));
  284. }
  285. return clones;
  286. };
  287. /**
  288. * Clone the object and delete internal properties.
  289. *
  290. * @param [ Object ] obj The object to clone for.
  291. *
  292. * @return [ Object ]
  293. */
  294. exports.clone = function (obj) {
  295. var ignore = ['action'],
  296. clone = {};
  297. for (var prop in obj) {
  298. if (ignore.includes(prop) || typeof obj[prop] === 'function')
  299. continue;
  300. try {
  301. clone[prop] = obj[prop];
  302. } catch (e) {
  303. clone[prop] = null;
  304. }
  305. }
  306. return clone;
  307. };
  308. /**
  309. * Parse notification spec into an instance of prefered type.
  310. *
  311. * @param [ Object ] obj The notification options map.
  312. *
  313. * @return [ LocalNotification.Options ]
  314. */
  315. exports.parseOptions = function (obj) {
  316. var opts = new LocalNotification.Options(),
  317. ignore = ['actions', 'trigger'];
  318. for (var prop in opts) {
  319. if (!ignore.includes(prop) && obj[prop]) {
  320. opts[prop] = obj[prop];
  321. }
  322. }
  323. var trigger = exports.parseTrigger(obj);
  324. opts.trigger = trigger;
  325. var actions = exports.parseActions(obj);
  326. opts.actions = actions;
  327. return opts;
  328. };
  329. /**
  330. * Parse trigger spec into instance of prefered type.
  331. *
  332. * @param [ Object ] obj The notification options map.
  333. *
  334. * @return [ LocalNotification.Trigger ]
  335. */
  336. exports.parseTrigger = function (obj) {
  337. var trigger = new LocalNotification.Trigger(),
  338. spec = obj.trigger, val;
  339. if (!spec) return trigger;
  340. for (var prop in trigger) {
  341. val = spec[prop];
  342. if (!val) continue;
  343. trigger[prop] = prop == 'every' ? val.toString() : val;
  344. }
  345. return trigger;
  346. };
  347. /**
  348. * Parse action specs into instances of prefered types.
  349. *
  350. * @param [ Object ] obj The notification options map.
  351. *
  352. * @return [ Array<LocalNotification.Action> ]
  353. */
  354. exports.parseActions = function (obj) {
  355. var actions = [];
  356. if (!obj.actions) return actions;
  357. for (var i = 0, action, btn; i < obj.actions.length; i++) {
  358. action = obj.actions[i];
  359. if (!action.type || action.type == 'button') {
  360. btn = new LocalNotification.Button();
  361. } else
  362. if (action.type == 'input') {
  363. btn = new LocalNotification.Input();
  364. }
  365. for (var prop in btn) {
  366. if (action[prop]) btn[prop] = action[prop];
  367. }
  368. actions.push(btn);
  369. }
  370. return actions;
  371. };
  372. // Handle onclick event
  373. document.addEventListener('activated', function (e) {
  374. if (e.kind == ActivationKind.toastNotification) {
  375. exports.clicked(e.raw.argument, e.raw.userInput);
  376. }
  377. }, false);
  378. cordova.commandProxy.add('LocalNotification', exports);