local-notification.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 (typeof options.json == 'object') {
  113. options.json = JSON.stringify(options.json);
  114. }
  115. if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
  116. fn = function (cmd) {
  117. eval(cmd);
  118. };
  119. }
  120. exec(fn, null, 'LocalNotification', 'add', [options]);
  121. return options.id;
  122. };
  123. /**
  124. * Cancels the specified notification.
  125. *
  126. * @param {String} id
  127. * The ID of the notification
  128. * @param {Function} callback
  129. * A function to be called after the notification has been canceled
  130. * @param {Object} scope
  131. * The scope for the callback function
  132. */
  133. exports.cancel = function (id, callback, scope) {
  134. var fn = this.createCallbackFn(callback, scope);
  135. exec(fn, null, 'LocalNotification', 'cancel', [(id || '0').toString()]);
  136. };
  137. /**
  138. * Removes all previously registered notifications.
  139. *
  140. * @param {Function} callback
  141. * A function to be called after all notifications have been canceled
  142. * @param {Object} scope
  143. * The scope for the callback function
  144. */
  145. exports.cancelAll = function (callback, scope) {
  146. var fn = this.createCallbackFn(callback, scope);
  147. exec(fn, null, 'LocalNotification', 'cancelAll', []);
  148. };
  149. /**
  150. * Retrieves a list with all currently pending notifications.
  151. *
  152. * @param {Function} callback
  153. * A callback function to be called with the list
  154. * @param {Object} scope
  155. * The scope for the callback function
  156. */
  157. exports.getScheduledIds = function (callback, scope) {
  158. var fn = this.createCallbackFn(callback, scope);
  159. exec(fn, null, 'LocalNotification', 'getScheduledIds', []);
  160. };
  161. /**
  162. * Checks wether a notification with an ID is scheduled.
  163. *
  164. * @param {String} id
  165. * The ID of the notification
  166. * @param {Function} callback
  167. * A callback function to be called with the list
  168. * @param {Object} scope
  169. * The scope for the callback function
  170. */
  171. exports.isScheduled = function (id, callback, scope) {
  172. var fn = this.createCallbackFn(callback, scope);
  173. exec(fn, null, 'LocalNotification', 'isScheduled', [id.toString()]);
  174. };
  175. /**
  176. * Retrieves a list with all triggered notifications.
  177. *
  178. * @param {Function} callback
  179. * A callback function to be called with the list
  180. * @param {Object} scope
  181. * The scope for the callback function
  182. */
  183. exports.getTriggeredIds = function (callback, scope) {
  184. var fn = this.createCallbackFn(callback, scope);
  185. exec(fn, null, 'LocalNotification', 'getTriggeredIds', []);
  186. };
  187. /**
  188. * Checks wether a notification with an ID was triggered.
  189. *
  190. * @param {String} id
  191. * The ID of the notification
  192. * @param {Function} callback
  193. * A callback function to be called with the list
  194. * @param {Object} scope
  195. * The scope for the callback function
  196. */
  197. exports.isTriggered = function (id, callback, scope) {
  198. var fn = this.createCallbackFn(callback, scope);
  199. exec(fn, null, 'LocalNotification', 'isTriggered', [id.toString()]);
  200. };
  201. /**
  202. * Informs if the app has the permission to show notifications.
  203. *
  204. * @param {Function} callback
  205. * The function to be exec as the callback
  206. * @param {Object?} scope
  207. * The callback function's scope
  208. */
  209. exports.hasPermission = function (callback, scope) {
  210. var fn = this.createCallbackFn(callback, scope);
  211. if (device.platform != 'iOS') {
  212. fn(true);
  213. return;
  214. }
  215. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  216. };
  217. /**
  218. * Register permission to show notifications if not already granted.
  219. *
  220. * @param {Function} callback
  221. * The function to be exec as the callback
  222. * @param {Object?} scope
  223. * The callback function's scope
  224. */
  225. exports.registerPermission = function (callback, scope) {
  226. var fn = this.createCallbackFn(callback, scope);
  227. if (device.platform != 'iOS') {
  228. fn(true);
  229. return;
  230. }
  231. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  232. };
  233. exports.promptForPermission = function (callback, scope) {
  234. console.warn('Depreated: Please use `notification.local.registerPermission` instead.');
  235. exports.registerPermission.apply(this, arguments);
  236. };
  237. /**
  238. * Occurs when a notification was added.
  239. *
  240. * @param {String} id
  241. * The ID of the notification
  242. * @param {String} state
  243. * Either "foreground" or "background"
  244. * @param {String} json
  245. * A custom (JSON) string
  246. */
  247. exports.onadd = function (id, state, json) {};
  248. /**
  249. * Occurs when the notification is triggered.
  250. *
  251. * @param {String} id
  252. * The ID of the notification
  253. * @param {String} state
  254. * Either "foreground" or "background"
  255. * @param {String} json
  256. * A custom (JSON) string
  257. */
  258. exports.ontrigger = function (id, state, json) {};
  259. /**
  260. * Fires after the notification was clicked.
  261. *
  262. * @param {String} id
  263. * The ID of the notification
  264. * @param {String} state
  265. * Either "foreground" or "background"
  266. * @param {String} json
  267. * A custom (JSON) string
  268. */
  269. exports.onclick = function (id, state, json) {};
  270. /**
  271. * Fires if the notification was canceled.
  272. *
  273. * @param {String} id
  274. * The ID of the notification
  275. * @param {String} state
  276. * Either "foreground" or "background"
  277. * @param {String} json
  278. * A custom (JSON) string
  279. */
  280. exports.oncancel = function (id, state, json) {};
  281. /**
  282. * @private
  283. *
  284. * Merges custom properties with the default values.
  285. *
  286. * @param {Object} options
  287. * Set of custom values
  288. *
  289. * @retrun {Object}
  290. * The merged property list
  291. */
  292. exports.mergeWithDefaults = function (options) {
  293. var defaults = this.getDefaults();
  294. for (var key in defaults) {
  295. if (options[key] === undefined) {
  296. options[key] = defaults[key];
  297. }
  298. }
  299. return options;
  300. };
  301. /**
  302. * @private
  303. *
  304. * Merges the platform specific properties into the default properties.
  305. *
  306. * @return {Object}
  307. * The default properties for the platform
  308. */
  309. exports.applyPlatformSpecificOptions = function () {
  310. var defaults = this._defaults;
  311. switch (device.platform) {
  312. case 'Android':
  313. defaults.icon = 'icon';
  314. defaults.smallIcon = null;
  315. defaults.ongoing = false;
  316. defaults.led = 'FFFFFF'; /*RRGGBB*/
  317. defaults.sound = 'TYPE_NOTIFICATION'; break;
  318. case 'iOS':
  319. defaults.sound = ''; break;
  320. case 'WinCE': case 'Win32NT':
  321. defaults.smallImage = null;
  322. defaults.image = null;
  323. defaults.wideImage = null;
  324. }
  325. return defaults;
  326. };
  327. /**
  328. * @private
  329. *
  330. * Creates a callback, which will be executed within a specific scope.
  331. *
  332. * @param {Function} callbackFn
  333. * The callback function
  334. * @param {Object} scope
  335. * The scope for the function
  336. *
  337. * @return {Function}
  338. * The new callback function
  339. */
  340. exports.createCallbackFn = function (callbackFn, scope) {
  341. if (typeof callbackFn != 'function')
  342. return;
  343. return function () {
  344. callbackFn.apply(scope || this, arguments);
  345. };
  346. };