local-notification.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * Merge settings with default values
  54. *
  55. * @param {Object} options
  56. * @retrun {Object}
  57. */
  58. mergeWithDefaults: function (options) {
  59. var defaults = this.getDefaults();
  60. for (var key in defaults) {
  61. if (options[key] === undefined) {
  62. options[key] = defaults[key];
  63. }
  64. }
  65. return options;
  66. },
  67. /**
  68. * @private
  69. */
  70. applyPlatformSpecificOptions: function () {
  71. var defaults = this._defaults;
  72. switch (device.platform) {
  73. case 'Android':
  74. defaults.id = 0;
  75. defaults.icon = 'icon';
  76. defaults.smallIcon = null;
  77. defaults.ongoing = false;
  78. defaults.sound = 'TYPE_NOTIFICATION'; break;
  79. case 'iOS':
  80. defaults.sound = ''; break;
  81. case 'WinCE': case 'Win32NT':
  82. defaults.smallImage = null;
  83. defaults.image = null;
  84. defaults.wideImage = null;
  85. };
  86. },
  87. /**
  88. * Add a new entry to the registry
  89. *
  90. * @param {Object} options
  91. * @return {Number} The notification's ID
  92. */
  93. add: function (options) {
  94. var options = this.mergeWithDefaults(options),
  95. callbackFn = null;
  96. if (options.id) {
  97. options.id = options.id.toString();
  98. }
  99. if (options.date === undefined) {
  100. options.date = new Date();
  101. }
  102. if (typeof options.date == 'object') {
  103. options.date = Math.round(options.date.getTime()/1000);
  104. }
  105. if (['WinCE', 'Win32NT'].indexOf(device.platform)) {
  106. callbackFn = function (cmd) {
  107. eval(cmd);
  108. };
  109. }
  110. cordova.exec(callbackFn, null, 'LocalNotification', 'add', [options]);
  111. return options.id;
  112. },
  113. /**
  114. * Cancels the specified notification
  115. *
  116. * @param {String} id of the notification
  117. */
  118. cancel: function (id) {
  119. cordova.exec(null, null, 'LocalNotification', 'cancel', [id.toString()]);
  120. },
  121. /**
  122. * Removes all previously registered notifications
  123. */
  124. cancelAll: function () {
  125. cordova.exec(null, null, 'LocalNotification', 'cancelAll', []);
  126. },
  127. /**
  128. * @async
  129. *
  130. * Retrieves a list with all currently pending notifications.
  131. *
  132. * @param {Function} callback
  133. */
  134. getScheduledIds: function (callback) {
  135. cordova.exec(callback, null, 'LocalNotification', 'getScheduledIds', []);
  136. },
  137. /**
  138. * @async
  139. *
  140. * Checks wether a notification with an ID is scheduled.
  141. *
  142. * @param {String} id
  143. * @param {Function} callback
  144. */
  145. isScheduled: function (id, callback) {
  146. cordova.exec(callback, null, 'LocalNotification', 'isScheduled', [id.toString()]);
  147. },
  148. /**
  149. * Occurs when a notification was added.
  150. *
  151. * @param {String} id The ID of the notification
  152. * @param {String} state Either "foreground" or "background"
  153. * @param {String} json A custom (JSON) string
  154. */
  155. onadd: function (id, state, json) {},
  156. /**
  157. * Occurs when the notification is triggered.
  158. *
  159. * @param {String} id The ID of the notification
  160. * @param {String} state Either "foreground" or "background"
  161. * @param {String} json A custom (JSON) string
  162. */
  163. ontrigger: function (id, state, json) {},
  164. /**
  165. * Fires after the notification was clicked.
  166. *
  167. * @param {String} id The ID of the notification
  168. * @param {String} state Either "foreground" or "background"
  169. * @param {String} json A custom (JSON) string
  170. */
  171. onclick: function (id, state, json) {},
  172. /**
  173. * Fires if the notification was canceled.
  174. *
  175. * @param {String} id The ID of the notification
  176. * @param {String} state Either "foreground" or "background"
  177. * @param {String} json A custom (JSON) string
  178. */
  179. oncancel: function (id, state, json) {}
  180. };
  181. var plugin = new LocalNotification(),
  182. channel = require('cordova/channel');
  183. channel.onCordovaReady.subscribe( function () {
  184. plugin.applyPlatformSpecificOptions();
  185. });
  186. channel.deviceready.subscribe( function () {
  187. cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
  188. });
  189. module.exports = plugin;