Cordova phonegap navigator.notification.alert不工作
标题不言自明,我不明白为什么 资料来源: www/index.html:Cordova phonegap navigator.notification.alert不工作,cordova,notifications,Cordova,Notifications,标题不言自明,我不明白为什么 资料来源: www/index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <meta name="viewport" content="user-scala
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
android/res/xml中的config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>Hello Cordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<feature name="App">
<param name="android-package" value="org.apache.cordova.App" />
</feature>
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.Notification" />
</feature>
<access origin="http://127.0.0.1*" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="permissions" value="none" />
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="8" />
<preference name="android-installLocation" value="auto" />
</widget>
如和中所述
原因是:index.html中phonegap*js或cordova*js的名称可能与assets/www目录中的文件名不匹配。但我在platforms/android/assets/www中只有phonegap.js和cordova.js,而且我的名字完全一样。。。我还是不明白
更新
如果我做了一个远程构建,应用程序与本地构建正常工作,它不会;尽管我已经安装了版本3中提到的插件,但我还是使用Cordova CLI 3.0.9,使用命令行工具并遵循此处列出的文档构建了您的项目。我使用了你的HTML和JS代码,对话框弹出得很好 在查看其他文件时,我注意到config.xml中存在差异;我的config.xml如下所示:
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>Hello Cordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<feature name="App">
<param name="android-package" value="org.apache.cordova.App" />
</feature>
<feature name="Vibration">
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<access origin="*" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
</widget>
请注意,我的文件中有org.apache.cordova.dialogs.Notification-由于某种原因,您的文件缺少dialogs名称空间。这是故意的吗?如果在名称空间中添加对话框,是否有效?如果使用最新的CLI版本重建,会怎么样 尝试将此功能添加到config.xml文件中
<feature name="Notification">
<param name="wp-package" value="Notification"/>
</feature>
…一个很好的建议是将config.xml文件复制到您的www目录。您所要做的就是将此功能添加到您的项目中。。。 站在phonegap项目文件夹而不是平台文件夹中,例如:cd MobileApplications/MyPhoneGapExample 然后,添加我安装的cordova插件,而不是关闭phonegap,因此我建议如果安装phonegap,则必须使用phonegap命令:cordova plugin add org.apache.cordova.dialogs 我希望它对你有用 PD:
如果有人不知道平台文件夹和项目文件夹之间有什么区别,下面的文档可以说明区别:我正在使用visual studio 2015,我通过添加通知插件解决了这个问题 双击项目中的config.xml,选择通知插件并安装
您将只看到在android模拟器或设备上工作,ripple不工作
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
//this.receivedEvent('deviceready');
console.log(navigator.notification);
navigator.notification.alert(
'You are the winner!', // message
alertDismissed, // callback
'Game Over', // title
'Done' // buttonName
);
//navigator.notification.beep(2);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};app.initialize();
设备上的Reinstal APK应用程序。只有在安装系统时,系统才同意权限。只有重建不起作用。重新安装后,权限将显示在屏幕上并处于活动状态。hmm正确,但如果我没有弄错的话,我已将这些权限粘贴到其中,并尝试运行它,但没有成功。让我再次测试,然后让您知道。您是否升级了Cordova CLI?如果是这样的话,是不是在config.xml中添加value=org.apache.cordova.dialogs.Notification/>?现在一切都正常了。这似乎是一个破碎的科尔多瓦装置。我已将其完全删除并重新安装,现在没有任何问题。显然,这就是config.xml没有达到应有水平的原因。
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>Hello Cordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<feature name="App">
<param name="android-package" value="org.apache.cordova.App" />
</feature>
<feature name="Vibration">
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<access origin="*" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
</widget>
<feature name="Notification">
<param name="wp-package" value="Notification"/>
</feature>
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
//this.receivedEvent('deviceready');
console.log(navigator.notification);
navigator.notification.alert(
'You are the winner!', // message
alertDismissed, // callback
'Game Over', // title
'Done' // buttonName
);
//navigator.notification.beep(2);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};app.initialize();