local-notification.js 10 KB

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