在Phonegap构建应用程序中未定义Phonegap函数

编程入门 行业动态 更新时间:2024-10-15 06:19:33
在Phonegap构建应用程序中未定义Phonegap函数 - pushNotifications也不起作用(Phonegap functions not defined in Phonegap build apps - also pushNotifications don't work)

我无法正确使用phonegap。 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命令为了向后兼容的原因添加了两个文件,至少我是这么理解的)。

所以我试图找出为什么设备对象是未定义的,即使phonegap.js文件存在于www文件夹中并包含在html文件中。 我想如果我能得到“alert(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>

如果有人能帮我解决这个问题,那将非常棒。

I am having trouble to get phonegap working properly. The phonegap function/objects don't seem to be working. Also push notifications don't work too even though I have included the plugin using the proper CLI command and have made sure that all the files are in the correct places according to the documentation. I have use javascript code from the PushNotifications plugin documentation so I assume it is correct also.

I have installed PhoneGap on Mac OS X 10.8.4 and created a new PhoneGap project using the CLI interface.

Then I wrote the HTML/CSS/JavaScript files for the app and placed them in the www directory. I used the following command to build and run the application on my android device:

phonegap local run android

It worked fine and the application launched on my device. Everything worked fine. Then I added some code that uses phonegap's functions/objects and tried to run it on android again. The app ran fine again, but this time the following code did not execute:

alert(device.platform);

Also the PushNotifications code did not execute too due to an error (device is not defined) I have tried to include cordova.js, phonegap.js, both of them at the same time or even none of them, but the result is still same.

I checked to see if the platforms/android/assets/www folder in the project directory contained the correct files, and it did. Both cordova.js and phonegap.js files were automatically added (phonegap build command adds both files for backward compatibility reasons, at least thats how I understood it).

So I am trying to figure out why device object is undefined even when phonegap.js file exists in the www folder and is included in the html file. I think if I can get the "alert(device.platform);" code working then the push notification code would work too, as it fails at the if statement that has to evaluate device.platform.

Here is the code for the index page:

<!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>

It would be really great if anyone could help me out on this one.

最满意答案

你使用哪种版本的phonegap?

如果v3那么你是否安装了'设备'插件?

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

Which version of phonegap are you using?

If v3 then did you install the 'device' plugin?

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

更多推荐

phonegap,device,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-02 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   函数   中未   定义   Phonegap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!