local-notification.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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._deafults = {
  20. message: '',
  21. title: '',
  22. autoCancel: false,
  23. ongoing: false,
  24. badge: 0,
  25. id: '0',
  26. json: '',
  27. repeat: ''
  28. };
  29. };
  30. LocalNotification.prototype = {
  31. /**
  32. * Gibt alle Standardeinstellungen an.
  33. *
  34. * @return {Object}
  35. */
  36. getDefaults: function () {
  37. return this._deafults;
  38. },
  39. /**
  40. * Überschreibt die Standardeinstellungen.
  41. *
  42. * @param {Object} defaults
  43. */
  44. setDefaults: function (newDefaults) {
  45. var defaults = this.getDefaults();
  46. for (var key in defaults) {
  47. if (newDefaults[key] !== undefined) {
  48. defaults[key] = newDefaults[key];
  49. }
  50. }
  51. },
  52. /**
  53. * @private
  54. * Merged die Eigenschaften mit den Standardwerten.
  55. *
  56. * @param {Object} options
  57. * @retrun {Object}
  58. */
  59. mergeWithDefaults: function (options) {
  60. var defaults = this.getDefaults();
  61. for (var key in defaults) {
  62. if (options[key] === undefined) {
  63. options[key] = defaults[key];
  64. }
  65. }
  66. return options;
  67. },
  68. /**
  69. * @private
  70. */
  71. applyPlatformSpecificOptions: function () {
  72. var defaults = this._deafults;
  73. switch (device.platform) {
  74. case 'Android':
  75. defaults.icon = 'icon';
  76. defaults.smallIcon = null;
  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. * Fügt einen neuen Eintrag zur Registry hinzu.
  88. *
  89. * @param {Object} options
  90. * @return {Number} Die ID der Notification
  91. */
  92. add: function (options) {
  93. var options = this.mergeWithDefaults(options);
  94. if (options.id) {
  95. options.id = options.id.toString();
  96. }
  97. if (options.date === undefined) {
  98. options.date = new Date();
  99. }
  100. if (typeof options.date == 'object') {
  101. options.date = Math.round(options.date.getTime()/1000);
  102. }
  103. cordova.exec(null, null, 'LocalNotification', 'add', [options]);
  104. return options.id;
  105. },
  106. /**
  107. * Entfernt die angegebene Notification.
  108. *
  109. * @param {String} id
  110. */
  111. cancel: function (id) {
  112. cordova.exec(null, null, 'LocalNotification', 'cancel', [id.toString()]);
  113. },
  114. /**
  115. * Entfernt alle registrierten Notifications.
  116. */
  117. cancelAll: function () {
  118. cordova.exec(null, null, 'LocalNotification', 'cancelAll', []);
  119. },
  120. /**
  121. * Occurs when a notification was added.
  122. *
  123. * @param {String} id The ID of the notification
  124. * @param {String} state Either "foreground" or "background"
  125. * @param {String} json A custom (JSON) string
  126. */
  127. onadd: function (id, state, json) {},
  128. /**
  129. * Occurs when the notification is triggered.
  130. *
  131. * @param {String} id The ID of the notification
  132. * @param {String} state Either "foreground" or "background"
  133. * @param {String} json A custom (JSON) string
  134. */
  135. ontrigger: function (id, state, json) {},
  136. /**
  137. * Fires after the notification was clicked.
  138. *
  139. * @param {String} id The ID of the notification
  140. * @param {String} state Either "foreground" or "background"
  141. * @param {String} json A custom (JSON) string
  142. */
  143. onclick: function (id, state, json) {},
  144. /**
  145. * Fires if the notification was canceled.
  146. *
  147. * @param {String} id The ID of the notification
  148. * @param {String} state Either "foreground" or "background"
  149. * @param {String} json A custom (JSON) string
  150. */
  151. oncancel: function (id, state, json) {}
  152. };
  153. var plugin = new LocalNotification();
  154. document.addEventListener('deviceready', function () {
  155. plugin.applyPlatformSpecificOptions();
  156. }, false);
  157. module.exports = plugin;