local-notification.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.sound = 'TYPE_NOTIFICATION'; break;
  87. case 'iOS':
  88. defaults.sound = ''; break;
  89. case 'WinCE': case 'Win32NT':
  90. defaults.smallImage = null;
  91. defaults.image = null;
  92. defaults.wideImage = null;
  93. };
  94. return defaults;
  95. },
  96. /**
  97. * @private
  98. *
  99. * Creates a callback, which will be executed within a specific scope.
  100. *
  101. * @param {Function} callbackFn
  102. * The callback function
  103. * @param {Object} scope
  104. * The scope for the function
  105. *
  106. * @return {Function}
  107. * The new callback function
  108. */
  109. createCallbackFn: function (callbackFn, scope) {
  110. return function () {
  111. callbackFn.apply(arguments, scope || this);
  112. }
  113. },
  114. /**
  115. * Add a new entry to the registry
  116. *
  117. * @param {Object} options
  118. * The notification properties
  119. *
  120. * @return {Number}
  121. * The notification's ID
  122. */
  123. add: function (options) {
  124. var options = this.mergeWithDefaults(options),
  125. callbackFn = null;
  126. if (options.id) {
  127. options.id = options.id.toString();
  128. }
  129. if (options.date === undefined) {
  130. options.date = new Date();
  131. }
  132. if (typeof options.date == 'object') {
  133. options.date = Math.round(options.date.getTime()/1000);
  134. }
  135. if (['WinCE', 'Win32NT'].indexOf(device.platform)) {
  136. callbackFn = function (cmd) {
  137. eval(cmd);
  138. };
  139. }
  140. cordova.exec(callbackFn, null, 'LocalNotification', 'add', [options]);
  141. return options.id;
  142. },
  143. /**
  144. * Cancels the specified notification.
  145. *
  146. * @param {String} id
  147. * The ID of the notification
  148. * @param {Function} callback
  149. * A function to be called after the notification has been canceled
  150. * @param {Object} scope
  151. * The scope for the callback function
  152. */
  153. cancel: function (id, callback, scope) {
  154. var id = id.toString(),
  155. callbackFn = this.createCallbackFn(callback, scope);
  156. cordova.exec(callbackFn, null, 'LocalNotification', 'cancel', [id]);
  157. },
  158. /**
  159. * Removes all previously registered notifications.
  160. *
  161. * @param {Function} callback
  162. * A function to be called after all notifications have been canceled
  163. * @param {Object} scope
  164. * The scope for the callback function
  165. */
  166. cancelAll: function (callback, scope) {
  167. var callbackFn = this.createCallbackFn(callback, scope);
  168. cordova.exec(callbackFn, null, 'LocalNotification', 'cancelAll', []);
  169. },
  170. /**
  171. * Retrieves a list with all currently pending notifications.
  172. *
  173. * @param {Function} callback
  174. * A callback function to be called with the list
  175. * @param {Object} scope
  176. * The scope for the callback function
  177. */
  178. getScheduledIds: function (callback, scope) {
  179. var callbackFn = this.createCallbackFn(callback, scope);
  180. cordova.exec(callbackFn, null, 'LocalNotification', 'getScheduledIds', []);
  181. },
  182. /**
  183. * Checks wether a notification with an ID is scheduled.
  184. *
  185. * @param {String} id
  186. * The ID of the notification
  187. * @param {Function} callback
  188. * A callback function to be called with the list
  189. * @param {Object} scope
  190. * The scope for the callback function
  191. */
  192. isScheduled: function (id, callback, scope) {
  193. var id = id.toString(),
  194. callbackFn = this.createCallbackFn(callback, scope);
  195. cordova.exec(callbackFn, null, 'LocalNotification', 'isScheduled', [id]);
  196. },
  197. /**
  198. * Occurs when a notification was added.
  199. *
  200. * @param {String} id
  201. * The ID of the notification
  202. * @param {String} state
  203. * Either "foreground" or "background"
  204. * @param {String} json
  205. * A custom (JSON) string
  206. */
  207. onadd: function (id, state, json) {},
  208. /**
  209. * Occurs when the notification is triggered.
  210. *
  211. * @param {String} id
  212. * The ID of the notification
  213. * @param {String} state
  214. * Either "foreground" or "background"
  215. * @param {String} json
  216. * A custom (JSON) string
  217. */
  218. ontrigger: function (id, state, json) {},
  219. /**
  220. * Fires after the notification was clicked.
  221. *
  222. * @param {String} id
  223. * The ID of the notification
  224. * @param {String} state
  225. * Either "foreground" or "background"
  226. * @param {String} json
  227. * A custom (JSON) string
  228. */
  229. onclick: function (id, state, json) {},
  230. /**
  231. * Fires if the notification was canceled.
  232. *
  233. * @param {String} id
  234. * The ID of the notification
  235. * @param {String} state
  236. * Either "foreground" or "background"
  237. * @param {String} json
  238. * A custom (JSON) string
  239. */
  240. oncancel: function (id, state, json) {}
  241. };
  242. var plugin = new LocalNotification(),
  243. channel = require('cordova/channel');
  244. // Called after all 'deviceready' listener are called
  245. channel.deviceready.subscribe( function () {
  246. // Device is ready now, the listeners are registered and all queued events
  247. // can be executed now.
  248. cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
  249. });
  250. channel.onCordovaReady.subscribe( function () {
  251. // The cordova device plugin is ready now
  252. channel.onCordovaInfoReady.subscribe( function () {
  253. if (device.platform == 'Android') {
  254. channel.onPause.subscribe( function () {
  255. // Necessary to set the state to `background`
  256. cordova.exec(null, null, 'LocalNotification', 'pause', []);
  257. });
  258. channel.onResume.subscribe( function () {
  259. // Necessary to set the state to `foreground`
  260. cordova.exec(null, null, 'LocalNotification', 'resume', []);
  261. });
  262. // Necessary to set the state to `foreground`
  263. cordova.exec(null, null, 'LocalNotification', 'resume', []);
  264. }
  265. // Merges the platform specific properties into the default properties
  266. plugin.applyPlatformSpecificOptions();
  267. });
  268. });
  269. module.exports = plugin;