local-notification.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. var LocalNotification = function () {
  19. this._defaults = {
  20. message: '',
  21. title: '',
  22. autoCancel: false,
  23. badge: 0,
  24. id: '0',
  25. json: '',
  26. repeat: ''
  27. };
  28. };
  29. LocalNotification.prototype = {
  30. /**
  31. * Returns the default settings
  32. *
  33. * @return {Object}
  34. */
  35. getDefaults: function () {
  36. return this._defaults;
  37. },
  38. /**
  39. * Overwrite default settings
  40. *
  41. * @param {Object} defaults
  42. */
  43. setDefaults: function (newDefaults) {
  44. var defaults = this.getDefaults();
  45. for (var key in defaults) {
  46. if (newDefaults[key] !== undefined) {
  47. defaults[key] = newDefaults[key];
  48. }
  49. }
  50. },
  51. /**
  52. * @private
  53. *
  54. * Merges custom properties with the default values.
  55. *
  56. * @param {Object} options
  57. * Set of custom values
  58. *
  59. * @retrun {Object}
  60. * The merged property list
  61. */
  62. mergeWithDefaults: function (options) {
  63. var defaults = this.getDefaults();
  64. for (var key in defaults) {
  65. if (options[key] === undefined) {
  66. options[key] = defaults[key];
  67. }
  68. }
  69. return options;
  70. },
  71. /**
  72. * @private
  73. *
  74. * Merges the platform specific properties into the default properties.
  75. *
  76. * @return {Object}
  77. * The default properties for the platform
  78. */
  79. applyPlatformSpecificOptions: function () {
  80. var defaults = this._defaults;
  81. switch (device.platform) {
  82. case 'Android':
  83. defaults.icon = 'icon';
  84. defaults.smallIcon = null;
  85. defaults.ongoing = false;
  86. defaults.led = 'FFFFFF'; /*RRGGBB*/
  87. defaults.sound = 'TYPE_NOTIFICATION'; break;
  88. case 'iOS':
  89. defaults.sound = ''; break;
  90. case 'WinCE': case 'Win32NT':
  91. defaults.smallImage = null;
  92. defaults.image = null;
  93. defaults.wideImage = null;
  94. }
  95. return defaults;
  96. },
  97. /**
  98. * @private
  99. *
  100. * Creates a callback, which will be executed within a specific scope.
  101. *
  102. * @param {Function} callbackFn
  103. * The callback function
  104. * @param {Object} scope
  105. * The scope for the function
  106. *
  107. * @return {Function}
  108. * The new callback function
  109. */
  110. createCallbackFn: function (callbackFn, scope) {
  111. if (typeof callbackFn != 'function')
  112. return;
  113. return function () {
  114. callbackFn.apply(scope || this, arguments);
  115. };
  116. },
  117. /**
  118. * Add a new entry to the registry
  119. *
  120. * @param {Object} options
  121. * The notification properties
  122. * @param {Function} callback
  123. * A function to be called after the notification has been canceled
  124. * @param {Object} scope
  125. * The scope for the callback function
  126. *
  127. * @return {Number}
  128. * The notification's ID
  129. */
  130. add: function (options, callback, scope) {
  131. var options = this.mergeWithDefaults(options),
  132. callbackFn = this.createCallbackFn(callback, scope);
  133. if (options.id) {
  134. options.id = options.id.toString();
  135. }
  136. if (options.date === undefined) {
  137. options.date = new Date();
  138. }
  139. if (options.title) {
  140. options.title = options.title.toString();
  141. }
  142. if (options.message) {
  143. options.message = options.message.toString();
  144. }
  145. if (typeof options.date == 'object') {
  146. options.date = Math.round(options.date.getTime()/1000);
  147. }
  148. if (['WinCE', 'Win32NT'].indexOf(device.platform)) {
  149. callbackFn = function (cmd) {
  150. eval(cmd);
  151. };
  152. }
  153. cordova.exec(callbackFn, null, 'LocalNotification', 'add', [options]);
  154. return options.id;
  155. },
  156. /**
  157. * Cancels the specified notification.
  158. *
  159. * @param {String} id
  160. * The ID of the notification
  161. * @param {Function} callback
  162. * A function to be called after the notification has been canceled
  163. * @param {Object} scope
  164. * The scope for the callback function
  165. */
  166. cancel: function (id, callback, scope) {
  167. var id = id.toString(),
  168. callbackFn = this.createCallbackFn(callback, scope);
  169. cordova.exec(callbackFn, null, 'LocalNotification', 'cancel', [id]);
  170. },
  171. /**
  172. * Removes all previously registered notifications.
  173. *
  174. * @param {Function} callback
  175. * A function to be called after all notifications have been canceled
  176. * @param {Object} scope
  177. * The scope for the callback function
  178. */
  179. cancelAll: function (callback, scope) {
  180. var callbackFn = this.createCallbackFn(callback, scope);
  181. cordova.exec(callbackFn, null, 'LocalNotification', 'cancelAll', []);
  182. },
  183. /**
  184. * Retrieves a list with all currently pending notifications.
  185. *
  186. * @param {Function} callback
  187. * A callback function to be called with the list
  188. * @param {Object} scope
  189. * The scope for the callback function
  190. */
  191. getScheduledIds: function (callback, scope) {
  192. var callbackFn = this.createCallbackFn(callback, scope);
  193. cordova.exec(callbackFn, null, 'LocalNotification', 'getScheduledIds', []);
  194. },
  195. /**
  196. * Checks wether a notification with an ID is scheduled.
  197. *
  198. * @param {String} id
  199. * The ID of the notification
  200. * @param {Function} callback
  201. * A callback function to be called with the list
  202. * @param {Object} scope
  203. * The scope for the callback function
  204. */
  205. isScheduled: function (id, callback, scope) {
  206. var id = id.toString(),
  207. callbackFn = this.createCallbackFn(callback, scope);
  208. cordova.exec(callbackFn, null, 'LocalNotification', 'isScheduled', [id]);
  209. },
  210. /**
  211. * Occurs when a notification was added.
  212. *
  213. * @param {String} id
  214. * The ID of the notification
  215. * @param {String} state
  216. * Either "foreground" or "background"
  217. * @param {String} json
  218. * A custom (JSON) string
  219. */
  220. onadd: function (id, state, json) {},
  221. /**
  222. * Occurs when the notification is triggered.
  223. *
  224. * @param {String} id
  225. * The ID of the notification
  226. * @param {String} state
  227. * Either "foreground" or "background"
  228. * @param {String} json
  229. * A custom (JSON) string
  230. */
  231. ontrigger: function (id, state, json) {},
  232. /**
  233. * Fires after the notification was clicked.
  234. *
  235. * @param {String} id
  236. * The ID of the notification
  237. * @param {String} state
  238. * Either "foreground" or "background"
  239. * @param {String} json
  240. * A custom (JSON) string
  241. */
  242. onclick: function (id, state, json) {},
  243. /**
  244. * Fires if the notification was canceled.
  245. *
  246. * @param {String} id
  247. * The ID of the notification
  248. * @param {String} state
  249. * Either "foreground" or "background"
  250. * @param {String} json
  251. * A custom (JSON) string
  252. */
  253. oncancel: function (id, state, json) {}
  254. };
  255. var plugin = new LocalNotification(),
  256. channel = require('cordova/channel');
  257. // Called after all 'deviceready' listener are called
  258. channel.deviceready.subscribe( function () {
  259. // Device is ready now, the listeners are registered and all queued events
  260. // can be executed now.
  261. cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
  262. });
  263. channel.onCordovaReady.subscribe( function () {
  264. // The cordova device plugin is ready now
  265. channel.onCordovaInfoReady.subscribe( function () {
  266. if (device.platform == 'Android') {
  267. channel.onPause.subscribe( function () {
  268. // Necessary to set the state to `background`
  269. cordova.exec(null, null, 'LocalNotification', 'pause', []);
  270. });
  271. channel.onResume.subscribe( function () {
  272. // Necessary to set the state to `foreground`
  273. cordova.exec(null, null, 'LocalNotification', 'resume', []);
  274. });
  275. // Necessary to set the state to `foreground`
  276. cordova.exec(null, null, 'LocalNotification', 'resume', []);
  277. }
  278. // Merges the platform specific properties into the default properties
  279. plugin.applyPlatformSpecificOptions();
  280. });
  281. });
  282. module.exports = plugin;