local-notification.js 6.2 KB

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