JQuery - 使用navigator.notification.alert

时间:2013-07-04 12:20:56

标签: jquery-mobile notifications cordova customization alert

我要求弹出窗口显示自定义标题(从应用程序的index.html看起来看起来很俗气)。

我在以下链接的末尾尝试了这个建议:

Custom JavaScript alerts in iOS using PhoneGap HTML

所以我在脚本部分的index.html中添加了以下代码:

   function showMessage(message, callback, title, buttonName){

        title = title || "default title";
        buttonName = buttonName || 'OK';

        if(navigator.notification && navigator.notification.alert){

            navigator.notification.alert(
                message,    // message
                callback,   // callback
                title,      // title
                buttonName  // buttonName
            );

        }else{

            alert(message);
            callback();
        }

    }

更新

我有以下警报代码;

if ((inputNumber>maxAllowed))
        {
        showMessage("The input is too high.",null,"Warning","Warning");
        }

编译应用程序后,这不起作用。

以下是index.html:

    document.addEventListener("deviceready", onDeviceReady, false);

            function onDeviceReady() {
            // Now safe to use the PhoneGap API
            }

<function shown above is here>

知道为什么这仍然不起作用?从index.html显示

谢谢。

亲切的问候,

Gary Shergill

5 个答案:

答案 0 :(得分:8)

此错误告诉您 navigator.notification 功能不存在。

通常这是因为:

  1. 未在HEAD
  2. 内初始化Phonegap / Cordova
  3. 在deviceready事件中未初始化函数。基本上,在完全初始化cordova.js之前无法调用函数。

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
        // Now safe to use the PhoneGap API
    }
    

答案 1 :(得分:3)

这是我在PC上测试Phonegap应用程序时使用的功能。我在移动设备上部署应用时将其删除。它用于确认功能,但您可以将其调整为警报等。

    // TODO: remove on deploy
    navigator.notification = {
        confirm: function (message, successCallback) {
            successCallback(1);
        }
    };

答案 2 :(得分:0)

您正在浏览器中进行测试,因此navigator.notification未定义。此外,您似乎添加了函数showMessage,但您没有使用它。试试:

showMessage("The value is too high!", null,"Warning", "Warning");

答案 3 :(得分:0)

从手机,请注意回调不是字符串。所以在你的函数中,你传递一个字符串,这会导致它出现问题。

http://docs.phonegap.com/en/1.0.0/phonegap_notification_notification.md.html

navigator.notification.alert(
    'You are the winner!',  // message
    alertDismissed,         // callback
    'Game Over',            // title
    'Done'                  // buttonName
);

我认为情况就是这样,因为我也试图这样做。所以很遗憾你无法覆盖回调,而你需要“硬编码”它。

答案 4 :(得分:0)

我已使用CLI添加插件,如:

$ cordova plugin add cordova-plugin-dialogs

它对我来说很好。