LocalNotificationProxy.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = [], actions = [];
  58. for (var i = 0, props, opts; i < args.length; i++) {
  59. props = args[i];
  60. opts = new LocalNotification.Options();
  61. for (var prop in opts) {
  62. if (prop != 'actions' && props[prop]) opts[prop] = props[prop];
  63. }
  64. for (var j = 0, action, btn; j < props.actions.length; j++) {
  65. action = props.actions[j];
  66. if (!action.type || action.type == 'button') {
  67. btn = new LocalNotification.Button();
  68. } else
  69. if (action.type == 'input') {
  70. btn = new LocalNotification.Input();
  71. }
  72. for (prop in btn) {
  73. if (action[prop]) btn[prop] = action[prop];
  74. }
  75. actions.push(btn);
  76. }
  77. opts.actions = actions;
  78. options.push(opts);
  79. }
  80. impl.schedule(options);
  81. success();
  82. };
  83. /**
  84. * List of all notification ids.
  85. *
  86. * @param [ Function ] success Success callback
  87. * @param [ Function ] error Error callback
  88. *
  89. * @return [ Void ]
  90. */
  91. exports.ids = function (success, error) {
  92. var ids = impl.ids() || [];
  93. success(Array.from(ids));
  94. };
  95. /**
  96. * List of all scheduled notification ids.
  97. *
  98. * @param [ Function ] success Success callback
  99. * @param [ Function ] error Error callback
  100. *
  101. * @return [ Void ]
  102. */
  103. exports.scheduledIds = function (success, error) {
  104. var ids = impl.scheduledIds() || [];
  105. success(Array.from(ids));
  106. };
  107. /**
  108. * List of all triggered notification ids.
  109. *
  110. * @param [ Function ] success Success callback
  111. * @param [ Function ] error Error callback
  112. *
  113. * @return [ Void ]
  114. */
  115. exports.triggeredIds = function (success, error) {
  116. var ids = impl.triggeredIds() || [];
  117. success(Array.from(ids));
  118. };
  119. /**
  120. * Get a single notification by id.
  121. *
  122. * @param [ Function ] success Success callback
  123. * @param [ Function ] error Error callback
  124. * @param [ Array ] args Interface arguments
  125. *
  126. * @return [ Void ]
  127. */
  128. exports.notification = function (success, error, args) {
  129. var obj = impl.notification(args[0]);
  130. success(obj);
  131. };
  132. /**
  133. * List of (all) notifications.
  134. *
  135. * @param [ Function ] success Success callback
  136. * @param [ Function ] error Error callback
  137. * @param [ Array ] args Interface arguments
  138. *
  139. * @return [ Void ]
  140. */
  141. exports.notifications = function (success, error, args) {
  142. var objs = impl.notifications(args) || [];
  143. success(Array.from(objs));
  144. };
  145. /**
  146. * List of all scheduled notifications.
  147. *
  148. * @param [ Function ] success Success callback
  149. * @param [ Function ] error Error callback
  150. *
  151. * @return [ Void ]
  152. */
  153. exports.scheduledNotifications = function (success, error) {
  154. var objs = impl.scheduledNotifications() || [];
  155. success(Array.from(objs));
  156. };
  157. /**
  158. * List of all triggered notifications.
  159. *
  160. * @param [ Function ] success Success callback
  161. * @param [ Function ] error Error callback
  162. *
  163. * @return [ Void ]
  164. */
  165. exports.triggeredNotifications = function (success, error) {
  166. var objs = impl.triggeredNotifications() || [];
  167. success(Array.from(objs));
  168. };
  169. /**
  170. * Inform the user through the click event that a notification was clicked.
  171. *
  172. * @param [ String ] xml The launch identifier.
  173. *
  174. * @return [ Void ]
  175. */
  176. exports.clicked = function (xml, input) {
  177. var toast = LocalNotification.Options.parse(xml),
  178. event = toast.action || 'click',
  179. e = { event: event };
  180. if (input && input.size > 0) {
  181. var it = input.first();
  182. e.text = it.current.value;
  183. while (it.hasCurrent) {
  184. e[it.current.key] = it.current.value;
  185. it.moveNext();
  186. }
  187. }
  188. cordova.plugins.notification.local.core.fireEvent(event, toast, e);
  189. };
  190. // Handle onclick event
  191. document.addEventListener('activated', function (e) {
  192. if (e.kind == ActivationKind.toastNotification) {
  193. exports.clicked(e.raw.argument, e.raw.userInput);
  194. }
  195. }, false);
  196. cordova.commandProxy.add('LocalNotification', exports);