【问题标题】:How to import the navigator.notification.alert plugin PhoneGap如何导入 navigator.notification.alert 插件 PhoneGap
【发布时间】:2024-04-29 19:00:01
【问题描述】:

我是 PhoneGap 的新手,我的 navigator.notification.alert 不起作用。 我认为这是因为我没有导入 navigator.notification.alert 插件,但我不知道如何在我的设置中导入。 这是我的 config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns = "http://www.w3.org/ns/widgets"
    xmlns:gap = "http://phonegap.com/ns/1.0"
    id        = "id"
    versionCode=""
    version   = "0.90">
    <name>Name</name>
    <description> Description </description>
    <author href="" email="">

    </author>
    <preference name="phonegap-version" value="cli-5.1.1" />
    <preference name="orientation" value="portrait" />
    <preference name="fullscreen" value="false" />
    <preference name="target-device" value="universal" />
    <preference name="webviewbounce" value="true" />
    <preference name="prerendered-icon" value="true" />
    <preference name="stay-in-webview" value="true" />
    <preference name="ios-statusbarstyle" value="default" />
    <preference name="detect-data-types" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="show-splash-screen-spinner" value="true" />
    <preference name="auto-hide-splash-screen" value="true" />
    <preference name="EnableViewportScale" value="true" />
    <preference name="MediaPlaybackRequiresUserAction" value="false" />
    <preference name="AllowInlineMediaPlayback" value="false" />
    <preference name="BackupWebStorage" value="cloud" />
    <preference name="TopActivityIndicator" value="gray" />
    <preference name="KeyboardDisplayRequiresUserAction" value="false" />
    <preference name="HideKeyboardFormAccessoryBar" value="false" />
    <preference name="SuppressesIncrementalRendering" value="false" />
    <preference name="android-minSdkVersion" value="7" />
    <preference name="android-installLocation" value="internalOnly" />
    <preference name="SplashScreenDelay" value="5000" />
    <preference name="ErrorUrl" value=""/>
    <preference name="BackgroundColor" value="0x000000"/>
    <preference name="KeepRunning" value="true"/>
    <preference name="DisallowOverscroll" value="false"/>
    <preference name="LoadingDialog" value=","/> 
    <preference name="LoadUrlTimeoutValue" value="20000" />
    <preference name="disable-cursor" value="false" />
    <gap:platform name="ios" />
    <gap:platform name="android" />
    <feature name="http://api.phonegap.com/1.0/notification"/>
    <gap:plugin name="org.apache.cordova.Notification" />
</widget>

这是我的 index.html:

<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-  1.4.5.min.css" />
<link rel="stylesheet" type="text/css" href="css/css.css" />
<script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="phonegap.js"></script>
</head>

<body onload="pageLoaded();">
<div id="content">
<script type="text/javascript">
function onclick1() {
    navigator.notification.alert("Hi");
}
</script>
<p><input id="Button1" type="button" value="Test 1" onclick="return onclick1()" /></p>
<script type="text/javascript">
function testAlert2() {
    navigator.notification.alert(
    "message",
    "title",
    "OK",
    {
        onClose: function(buttonIndex) {
            if (buttonIndex == 0)
                //doSomething();
            }
        }
    );
}
</script>
<p><a href="javascript: testAlert2();">Test 2</a></p>


<script type="text/javascript">
function Alert3() {
    navigator.notification.alert("message", function() {}, "title", "OK");
}
</script>
<p><a href="javascript: Alert3();">Test 3</a></p>


<script type="text/javascript">
function Alert4() {
    navigator.notification.alert('Title','Message','Button message');
}
</script>
<p><a href="javascript: Alert4();">Test 4</a></p>



</div>
</div>
</body>
</html>

在我的 config.xml 中,我将如何导入 navigator.notification.alert? 这些警报都不起作用。

【问题讨论】:

    标签: javascript android cordova plugins


    【解决方案1】:

    添加插件:

    cordova plugin add org.apache.cordova.dialogs
    

    在您的 config.xml 中:

    <feature name="Notification">
    <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
    </feature>
    

    示例:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Notification Example</title>
    
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    
    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);
    
    // device APIs are available
    //
    function onDeviceReady() {
        // Empty
    }
    
    // alert dialog dismissed
        function alertDismissed() {
            // do something
        }
    
    // Show a custom alertDismissed
    //
    function showAlert() {
        navigator.notification.alert(
            'You are the winner!',  // message
            alertDismissed,         // callback
            'Game Over',            // title
            'Done'                  // buttonName
        );
    }
    
    </script>
    </head>
    <body>
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
    </body>
    </html>
    

    Details

    【讨论】:

    • 我在 之前复制并粘贴了该代码,但似乎没有任何改变。
    • 在 nodejs 中?不,每次我尝试执行 npm 时都会显示:
    • 没有用命令行添加的插件是不能使用的..在config.xml里写代码是不够的
    • npm 应该在节点 repl 之外运行
    • 好的,我想通了。谢谢。我在命令行中输入什么?我很抱歉这些问题。