local-notification.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 exec = require('cordova/exec'),
  19. channel = require('cordova/channel');
  20. // Called after 'deviceready' event
  21. channel.deviceready.subscribe( function () {
  22. // Device is ready now, the listeners are registered
  23. // and all queued events can be executed.
  24. exec(null, null, 'LocalNotification', 'deviceready', []);
  25. });
  26. // Called before 'deviceready' event
  27. channel.onCordovaReady.subscribe( function () {
  28. // The cordova device plugin is ready now
  29. channel.onCordovaInfoReady.subscribe( function () {
  30. if (device.platform == 'Android') {
  31. channel.onPause.subscribe( function () {
  32. // Necessary to set the state to `background`
  33. exec(null, null, 'LocalNotification', 'pause', []);
  34. });
  35. channel.onResume.subscribe( function () {
  36. // Necessary to set the state to `foreground`
  37. exec(null, null, 'LocalNotification', 'resume', []);
  38. });
  39. // Necessary to set the state to `foreground`
  40. exec(null, null, 'LocalNotification', 'resume', []);
  41. }
  42. // Merges the platform specific properties into the default properties
  43. exports.applyPlatformSpecificOptions();
  44. });
  45. });
  46. /**
  47. * @private
  48. *
  49. * Default values.
  50. */
  51. exports._defaults = {
  52. message: '',
  53. title: '',
  54. autoCancel: false,
  55. badge: 0,
  56. id: '0',
  57. json: '',
  58. repeat: ''
  59. };
  60. /**
  61. * Returns the default settings
  62. *
  63. * @return {Object}
  64. */
  65. exports.getDefaults = function () {
  66. return this._defaults;
  67. };
  68. /**
  69. * Overwrite default settings
  70. *
  71. * @param {Object} defaults
  72. */
  73. exports.setDefaults = function (newDefaults) {
  74. var defaults = this.getDefaults();
  75. for (var key in defaults) {
  76. if (newDefaults[key] !== undefined) {
  77. defaults[key] = newDefaults[key];
  78. }
  79. }
  80. };
  81. /**
  82. * Add a new entry to the registry
  83. *
  84. * @param {Object} props
  85. * The notification properties
  86. * @param {Function} callback
  87. * A function to be called after the notification has been canceled
  88. * @param {Object} scope
  89. * The scope for the callback function
  90. *
  91. * @return {Number}
  92. * The notification's ID
  93. */
  94. exports.add = function (props, callback, scope) {
  95. var options = this.mergeWithDefaults(props),
  96. fn = this.createCallbackFn(callback, scope);
  97. if (options.id) {
  98. options.id = options.id.toString();
  99. }
  100. if (options.date === undefined) {
  101. options.date = new Date();
  102. }
  103. if (options.title) {
  104. options.title = options.title.toString();
  105. }
  106. if (options.message) {
  107. options.message = options.message.toString();
  108. }
  109. if (typeof options.date == 'object') {
  110. options.date = Math.round(options.date.getTime()/1000);
  111. }
  112. if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
  113. fn = function (cmd) {
  114. eval(cmd);
  115. };
  116. }
  117. exec(fn, null, 'LocalNotification', 'add', [options]);
  118. return options.id;
  119. };
  120. /**
  121. * Cancels the specified notification.
  122. *
  123. * @param {String} id
  124. * The ID of the notification
  125. * @param {Function} callback
  126. * A function to be called after the notification has been canceled
  127. * @param {Object} scope
  128. * The scope for the callback function
  129. */
  130. exports.cancel = function (id, callback, scope) {
  131. var fn = this.createCallbackFn(callback, scope);
  132. exec(fn, null, 'LocalNotification', 'cancel', [id.toString()]);
  133. };
  134. /**
  135. * Removes all previously registered notifications.
  136. *
  137. * @param {Function} callback
  138. * A function to be called after all notifications have been canceled
  139. * @param {Object} scope
  140. * The scope for the callback function
  141. */
  142. exports.cancelAll = function (callback, scope) {
  143. var fn = this.createCallbackFn(callback, scope);
  144. exec(fn, null, 'LocalNotification', 'cancelAll', []);
  145. };
  146. /**
  147. * Retrieves a list with all currently pending notifications.
  148. *
  149. * @param {Function} callback
  150. * A callback function to be called with the list
  151. * @param {Object} scope
  152. * The scope for the callback function
  153. */
  154. exports.getScheduledIds = function (callback, scope) {
  155. var fn = this.createCallbackFn(callback, scope);
  156. exec(fn, null, 'LocalNotification', 'getScheduledIds', []);
  157. };
  158. /**
  159. * Checks wether a notification with an ID is scheduled.
  160. *
  161. * @param {String} id
  162. * The ID of the notification
  163. * @param {Function} callback
  164. * A callback function to be called with the list
  165. * @param {Object} scope
  166. * The scope for the callback function
  167. */
  168. exports.isScheduled = function (id, callback, scope) {
  169. var fn = this.createCallbackFn(callback, scope);
  170. exec(fn, null, 'LocalNotification', 'isScheduled', [id.toString()]);
  171. };
  172. /**
  173. * Retrieves a list with all triggered notifications.
  174. *
  175. * @param {Function} callback
  176. * A callback function to be called with the list
  177. * @param {Object} scope
  178. * The scope for the callback function
  179. */
  180. exports.getTriggeredIds = function (callback, scope) {
  181. var fn = this.createCallbackFn(callback, scope);
  182. exec(fn, null, 'LocalNotification', 'getTriggeredIds', []);
  183. };
  184. /**
  185. * Checks wether a notification with an ID was triggered.
  186. *
  187. * @param {String} id
  188. * The ID of the notification
  189. * @param {Function} callback
  190. * A callback function to be called with the list
  191. * @param {Object} scope
  192. * The scope for the callback function
  193. */
  194. exports.isTriggered = function (id, callback, scope) {
  195. var fn = this.createCallbackFn(callback, scope);
  196. exec(fn, null, 'LocalNotification', 'isTriggered', [id.toString()]);
  197. };
  198. /**
  199. * Informs if the app has the permission to show notifications.
  200. *
  201. * @param {Function} callback
  202. * The function to be exec as the callback
  203. * @param {Object?} scope
  204. * The callback function's scope
  205. */
  206. exports.hasPermission = function (callback, scope) {
  207. var fn = this.createCallbackFn(callback, scope);
  208. if (device.platform != 'iOS') {
  209. fn(true);
  210. return;
  211. }
  212. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  213. };
  214. /**
  215. * Ask for permission to show notifications if not already granted.
  216. *
  217. * @param {Function} callback
  218. * The function to be exec as the callback
  219. * @param {Object?} scope
  220. * The callback function's scope
  221. */
  222. exports.promptForPermission = function (callback, scope) {
  223. if (device.platform != 'iOS')
  224. return;
  225. exec(null, null, 'LocalNotification', 'promptForPermission', []);
  226. };
  227. /**
  228. * Occurs when a notification was added.
  229. *
  230. * @param {String} id
  231. * The ID of the notification
  232. * @param {String} state
  233. * Either "foreground" or "background"
  234. * @param {String} json
  235. * A custom (JSON) string
  236. */
  237. exports.onadd = function (id, state, json) {};
  238. /**
  239. * Occurs when the notification is triggered.
  240. *
  241. * @param {String} id
  242. * The ID of the notification
  243. * @param {String} state
  244. * Either "foreground" or "background"
  245. * @param {String} json
  246. * A custom (JSON) string
  247. */
  248. exports.ontrigger = function (id, state, json) {};
  249. /**
  250. * Fires after the notification was clicked.
  251. *
  252. * @param {String} id
  253. * The ID of the notification
  254. * @param {String} state
  255. * Either "foreground" or "background"
  256. * @param {String} json
  257. * A custom (JSON) string
  258. */
  259. exports.onclick = function (id, state, json) {};
  260. /**
  261. * Fires if the notification was canceled.
  262. *
  263. * @param {String} id
  264. * The ID of the notification
  265. * @param {String} state
  266. * Either "foreground" or "background"
  267. * @param {String} json
  268. * A custom (JSON) string
  269. */
  270. exports.oncancel = function (id, state, json) {};
  271. /**
  272. * @private
  273. *
  274. * Merges custom properties with the default values.
  275. *
  276. * @param {Object} options
  277. * Set of custom values
  278. *
  279. * @retrun {Object}
  280. * The merged property list
  281. */
  282. exports.mergeWithDefaults = function (options) {
  283. var defaults = this.getDefaults();
  284. for (var key in defaults) {
  285. if (options[key] === undefined) {
  286. options[key] = defaults[key];
  287. }
  288. }
  289. return options;
  290. };
  291. /**
  292. * @private
  293. *
  294. * Merges the platform specific properties into the default properties.
  295. *
  296. * @return {Object}
  297. * The default properties for the platform
  298. */
  299. exports.applyPlatformSpecificOptions = function () {
  300. var defaults = this._defaults;
  301. switch (device.platform) {
  302. case 'Android':
  303. defaults.icon = 'icon';
  304. defaults.smallIcon = null;
  305. defaults.ongoing = false;
  306. defaults.led = 'FFFFFF'; /*RRGGBB*/
  307. defaults.sound = 'TYPE_NOTIFICATION'; break;
  308. case 'iOS':
  309. defaults.sound = ''; break;
  310. case 'WinCE': case 'Win32NT':
  311. defaults.smallImage = null;
  312. defaults.image = null;
  313. defaults.wideImage = null;
  314. }
  315. return defaults;
  316. };
  317. /**
  318. * @private
  319. *
  320. * Creates a callback, which will be executed within a specific scope.
  321. *
  322. * @param {Function} callbackFn
  323. * The callback function
  324. * @param {Object} scope
  325. * The scope for the function
  326. *
  327. * @return {Function}
  328. * The new callback function
  329. */
  330. exports.createCallbackFn = function (callbackFn, scope) {
  331. if (typeof callbackFn != 'function')
  332. return;
  333. return function () {
  334. callbackFn.apply(scope || this, arguments);
  335. };
  336. };