local-notification.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (device.platform != 'iOS')
  227. return;
  228. exec(null, null, 'LocalNotification', 'registerPermission', []);
  229. };
  230. exports.promptForPermission = function (callback, scope) {
  231. console.warn('Depreated: Please use `notification.local.registerPermission` instead.');
  232. exports.registerPermission.apply(this, arguments);
  233. };
  234. /**
  235. * Occurs when a notification was added.
  236. *
  237. * @param {String} id
  238. * The ID of the notification
  239. * @param {String} state
  240. * Either "foreground" or "background"
  241. * @param {String} json
  242. * A custom (JSON) string
  243. */
  244. exports.onadd = function (id, state, json) {};
  245. /**
  246. * Occurs when the notification is triggered.
  247. *
  248. * @param {String} id
  249. * The ID of the notification
  250. * @param {String} state
  251. * Either "foreground" or "background"
  252. * @param {String} json
  253. * A custom (JSON) string
  254. */
  255. exports.ontrigger = function (id, state, json) {};
  256. /**
  257. * Fires after the notification was clicked.
  258. *
  259. * @param {String} id
  260. * The ID of the notification
  261. * @param {String} state
  262. * Either "foreground" or "background"
  263. * @param {String} json
  264. * A custom (JSON) string
  265. */
  266. exports.onclick = function (id, state, json) {};
  267. /**
  268. * Fires if the notification was canceled.
  269. *
  270. * @param {String} id
  271. * The ID of the notification
  272. * @param {String} state
  273. * Either "foreground" or "background"
  274. * @param {String} json
  275. * A custom (JSON) string
  276. */
  277. exports.oncancel = function (id, state, json) {};
  278. /**
  279. * @private
  280. *
  281. * Merges custom properties with the default values.
  282. *
  283. * @param {Object} options
  284. * Set of custom values
  285. *
  286. * @retrun {Object}
  287. * The merged property list
  288. */
  289. exports.mergeWithDefaults = function (options) {
  290. var defaults = this.getDefaults();
  291. for (var key in defaults) {
  292. if (options[key] === undefined) {
  293. options[key] = defaults[key];
  294. }
  295. }
  296. return options;
  297. };
  298. /**
  299. * @private
  300. *
  301. * Merges the platform specific properties into the default properties.
  302. *
  303. * @return {Object}
  304. * The default properties for the platform
  305. */
  306. exports.applyPlatformSpecificOptions = function () {
  307. var defaults = this._defaults;
  308. switch (device.platform) {
  309. case 'Android':
  310. defaults.icon = 'icon';
  311. defaults.smallIcon = null;
  312. defaults.ongoing = false;
  313. defaults.led = 'FFFFFF'; /*RRGGBB*/
  314. defaults.sound = 'TYPE_NOTIFICATION'; break;
  315. case 'iOS':
  316. defaults.sound = ''; break;
  317. case 'WinCE': case 'Win32NT':
  318. defaults.smallImage = null;
  319. defaults.image = null;
  320. defaults.wideImage = null;
  321. }
  322. return defaults;
  323. };
  324. /**
  325. * @private
  326. *
  327. * Creates a callback, which will be executed within a specific scope.
  328. *
  329. * @param {Function} callbackFn
  330. * The callback function
  331. * @param {Object} scope
  332. * The scope for the function
  333. *
  334. * @return {Function}
  335. * The new callback function
  336. */
  337. exports.createCallbackFn = function (callbackFn, scope) {
  338. if (typeof callbackFn != 'function')
  339. return;
  340. return function () {
  341. callbackFn.apply(scope || this, arguments);
  342. };
  343. };