notification.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *
  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. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var exec = require('cordova/exec');
  22. var platform = require('cordova/platform');
  23. /**
  24. * Provides access to notifications on the device.
  25. */
  26. module.exports = {
  27. /**
  28. * Open a native alert dialog, with a customizable title and button text.
  29. *
  30. * @param {String} message Message to print in the body of the alert
  31. * @param {Function} completeCallback The callback that is called when user clicks on a button.
  32. * @param {String} title Title of the alert dialog (default: Alert)
  33. * @param {String} buttonLabel Label of the close button (default: OK)
  34. */
  35. alert: function(message, completeCallback, title, buttonLabel) {
  36. var _message = (typeof message === "string" ? message : JSON.stringify(message));
  37. var _title = (typeof title === "string" ? title : "Alert");
  38. var _buttonLabel = (buttonLabel && typeof buttonLabel === "string" ? buttonLabel : "OK");
  39. exec(completeCallback, null, "Notification", "alert", [_message, _title, _buttonLabel]);
  40. },
  41. /**
  42. * Open a native confirm dialog, with a customizable title and button text.
  43. * The result that the user selects is returned to the result callback.
  44. *
  45. * @param {String} message Message to print in the body of the alert
  46. * @param {Function} resultCallback The callback that is called when user clicks on a button.
  47. * @param {String} title Title of the alert dialog (default: Confirm)
  48. * @param {Array} buttonLabels Array of the labels of the buttons (default: ['OK', 'Cancel'])
  49. */
  50. confirm: function(message, resultCallback, title, buttonLabels) {
  51. var _message = (typeof message === "string" ? message : JSON.stringify(message));
  52. var _title = (typeof title === "string" ? title : "Confirm");
  53. var _buttonLabels = (buttonLabels || ["OK", "Cancel"]);
  54. // Strings are deprecated!
  55. if (typeof _buttonLabels === 'string') {
  56. console.log("Notification.confirm(string, function, string, string) is deprecated. Use Notification.confirm(string, function, string, array).");
  57. }
  58. _buttonLabels = convertButtonLabels(_buttonLabels);
  59. exec(resultCallback, null, "Notification", "confirm", [_message, _title, _buttonLabels]);
  60. },
  61. /**
  62. * Open a native prompt dialog, with a customizable title and button text.
  63. * The following results are returned to the result callback:
  64. * buttonIndex Index number of the button selected.
  65. * input1 The text entered in the prompt dialog box.
  66. *
  67. * @param {String} message Dialog message to display (default: "Prompt message")
  68. * @param {Function} resultCallback The callback that is called when user clicks on a button.
  69. * @param {String} title Title of the dialog (default: "Prompt")
  70. * @param {Array} buttonLabels Array of strings for the button labels (default: ["OK","Cancel"])
  71. * @param {String} defaultText Textbox input value (default: empty string)
  72. */
  73. prompt: function(message, resultCallback, title, buttonLabels, defaultText) {
  74. var _message = (typeof message === "string" ? message : JSON.stringify(message));
  75. var _title = (typeof title === "string" ? title : "Prompt");
  76. var _buttonLabels = (buttonLabels || ["OK","Cancel"]);
  77. // Strings are deprecated!
  78. if (typeof _buttonLabels === 'string') {
  79. console.log("Notification.prompt(string, function, string, string) is deprecated. Use Notification.confirm(string, function, string, array).");
  80. }
  81. _buttonLabels = convertButtonLabels(_buttonLabels);
  82. var _defaultText = (defaultText || "");
  83. exec(resultCallback, null, "Notification", "prompt", [_message, _title, _buttonLabels, _defaultText]);
  84. },
  85. /**
  86. * Causes the device to beep.
  87. * On Android, the default notification ringtone is played "count" times.
  88. *
  89. * @param {Integer} count The number of beeps.
  90. */
  91. beep: function(count) {
  92. var defaultedCount = count || 1;
  93. exec(null, null, "Notification", "beep", [ defaultedCount ]);
  94. }
  95. };
  96. function convertButtonLabels(buttonLabels) {
  97. // Some platforms take an array of button label names.
  98. // Other platforms take a comma separated list.
  99. // For compatibility, we convert to the desired type based on the platform.
  100. if (platform.id == "amazon-fireos" || platform.id == "android" || platform.id == "ios" ||
  101. platform.id == "windowsphone" || platform.id == "firefoxos" || platform.id == "ubuntu" ||
  102. platform.id == "windows8" || platform.id == "windows") {
  103. if (typeof buttonLabels === 'string') {
  104. buttonLabels = buttonLabels.split(","); // not crazy about changing the var type here
  105. }
  106. } else {
  107. if (Array.isArray(buttonLabels)) {
  108. var buttonLabelArray = buttonLabels;
  109. buttonLabels = buttonLabelArray.toString();
  110. }
  111. }
  112. return buttonLabels;
  113. }