我无法正常使用phonegap。手机功能/对象似乎不起作用。即使我使用正确的CLI命令包含插件并且根据文档确保所有文件都在正确的位置,推送通知也不起作用。我使用了PushNotifications插件文档中的javascript代码,所以我认为它也是正确的。
我在Mac OS X 10.8.4上安装了PhoneGap,并使用CLI界面创建了一个新的PhoneGap项目。
然后我为应用程序编写了HTML / CSS / JavaScript文件并将它们放在www目录中。 我使用以下命令在我的Android设备上构建和运行应用程序:
phonegap local run android
它工作正常,应用程序在我的设备上启动。一切都很好。 然后我添加了一些使用phonegap函数/对象的代码,并尝试再次在android上运行它。 该应用程序再次运行正常,但这次没有执行以下代码:
alert(device.platform);
由于错误(设备未定义),PushNotifications代码也没有执行 我试图同时包括cordova.js,phonegap.js,但两者都没有,但结果仍然相同。
我检查项目目录中的platforms / android / assets / www文件夹是否包含正确的文件,它确实存在。 cordova.js和phonegap.js文件都是自动添加的(phonegap build命令添加了两个文件是出于向后兼容的原因,至少这是我理解的。)
所以我试图找出为什么设备对象未定义,即使在www文件夹中存在phonegap.js文件并且包含在html文件中。我想如果我能得到“警报(device.platform);”代码工作然后推送通知代码也会工作,因为它在必须评估device.platform的if语句失败。
以下是索引页面的代码:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/index.css"/>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/functions.js"></script>
<script src="js/fastclick.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript" src="http://debug.build.phonegap.com/target/target-script-min.js#f997ffa0-5ed6-11e2-84ec-12313d1744da"></script>
<script type="text/javascript" charset="utf-8">
//*********************************************************
// Wait for Cordova to Load
//*********************************************************
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
//THE FOLLOWING CODE IS RESPONSIBLE FOR PUSH NOTIFICATIONS
var pushNotification;
alert(device.platform);
try {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
$("#app-status-ul").append('<li>registering android</li>');
pushNotification.register(successHandler, errorHandler, {"senderID":"hidden-by-me","ecb":"onNotificationGCM"}); // required!
} else {
$("#app-status-ul").append('<li>registering iOS</li>');
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
}
}
catch(err) {
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
alert(txt);
}
//Rest of the code
updateData();
if (window.localStorage.getItem("default-school") == "infant") {
window.location.replace("infant.html");
} else
if (window.localStorage.getItem("default-school") == "junior") {
window.location.replace("junior.html");
};
}
// iOS
function onNotificationAPN(event) {
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (event.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
// Android
function onNotificationGCM(e) {
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
switch( e.event ) {
case 'registered':
if ( e.regid.length > 0 ) {
$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log("regID = " + e.regID);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/"+e.soundname);
my_media.play();
}
else {
// otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart) $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
else $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
}
$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
break;
case 'error':
$("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
break;
default:
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
function tokenHandler (result) {
$("#app-status-ul").append('<li>token: '+ result +'</li>');
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
function successHandler (result) {
$("#app-status-ul").append('<li>success:'+ result +'</li>');
}
function errorHandler (error) {
$("#app-status-ul").append('<li>error:'+ error +'</li>');
}
</script>
</head>
<body onload="initFastButtons();init();">
<span id="fastclick">
<div id="main">
<ul id="app-status-ul">
<li>Push Plugin test</li>
</ul>
</div>
</span>
</body>
</html>
如果有人能帮我解决这个问题,真的很棒。
答案 0 :(得分:8)
您使用的是哪个版本的手机屏幕?
如果v3那么你安装了“设备”插件吗?
$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
答案 1 :(得分:0)
我试图让它在Phonegap Build中工作很长时间,最后想出来了:
config.xml中
<gap:plugin name="org.apache.cordova.device" /> <!-- Needed to use device.model (Not available until document deviceready event-->
的javascript:
function deviceReady() {
alert(device.model);
}
document.addEventListener("deviceready", deviceReady, false);
但是,我发现我正在寻找的信息(device.model和device.version)不需要这个对象,因为它在 navigator.userAgent 中可用。 该版本是useragent字符串中的Android版本号,设备模型位于useragent字符串中的“Android”之后。