3

我无法让 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>

如果有人能帮我解决这个问题,那就太好了。

4

2 回答 2

8

你用的是哪个版本的phonegap?

如果是 v3,那么您是否安装了“设备”插件?

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
于 2013-10-01T13:53:38.167 回答
0

我试图让这个在Phonegap Build中工作很长时间,终于弄清楚了:

配置文件

<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中可用。版本是用户代理字符串中的 Android 版本号,设备型号紧随用户代理字符串中的“Android”之后。

于 2015-06-30T03:31:27.983 回答