【问题标题】:Phonegap functions not defined in Phonegap build apps - also pushNotifications don't workPhonegap 构建应用程序中未定义的 Phonegap 功能 - pushNotifications 也不起作用
【发布时间】:2015-06-30 11:31:27
【问题描述】:

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

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

【问题讨论】:

    标签: javascript android cordova


    【解决方案1】:

    您使用的是哪个版本的 phonegap?

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

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

    【讨论】:

    • 我实际上使用的是版本 3。它有效!我所要做的就是添加设备插件。很奇怪,我不记得在推送通知插件中看到任何提及“设备”插件。无论如何,非常感谢队友。我已经坚持了很长时间:)
    • 我被同一个问题抓到了!很高兴为您解决了问题。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 2016-06-23
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多