我发动PhoneGap的推送通知的应用程序时,不定义设备错误弹出 [英] Device is not defined error pops up when launching my Phonegap Push notification app

查看:108
本文介绍了我发动PhoneGap的推送通知的应用程序时,不定义设备错误弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的示例源$ C ​​$ C(的https:/ /github.com/phonegap-build/PushPlugin/tree/master/Example/www )。问题是,当我安装我的应用程序并启动它在我的Andr​​oid手机(或Android SDK模拟器),它未能注册设备到Android GCM。所有的时间错误弹出, - 设备没有定义。我不知道到底怎么能赶上错误,为什么不能注册设备。

步骤,我preformed是:


  1. - 创建的PhoneGap项目


  2. - 安装以下3个插件(科尔多瓦 - 插件 - device.git,科尔多瓦 - 插件 - media.git,PushPlugin)


  3. - 注册的应用程序在谷歌Android GCM和检索我的Goolge的云API和项目编号

  4. 找到已安装到我的项目根/ plugins文件夹中的文件PushNotification.js。该文件被复制到我的项目根/ www文件夹,并从index.html的
  5. 引用
  6. 添加在我的索引文件

  7. 添加到我的WWW / config.xml文件

  8. 新增jQuery Mobile的JS + CSS文件到我的项目

  9. 在我的HTML code变更senderID:1111146417746

  10. 建造应用与CLI - 的PhoneGap构建Android

和这里是我的index.html code:

 < HTML和GT;
    < HEAD>
        <标题> com.PhoneGap.c2dm< /标题>
    < /头>
    <身体GT;
        <! - <脚本类型=文/ JavaScript的字符集=utf-8SRC =科尔多瓦-2.2.0.js>< / SCRIPT> - >
     <链接rel =stylesheet属性HREF =EXT / jQuery的/ jquery.mobile-min.css>
    <脚本类型=文/ JavaScript的SRC =EXT / jQuery的/ jQuery的-min.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的SRC =EXT / jQuery的/ jquery.mobile-min.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的SRC =EXT / jquery.qr code.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的字符集=utf-8SRC =cordova.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的SRC =PushNotification.js>< / SCRIPT>        <脚本类型=文/ JavaScript的>
            VAR pushNotification;            功能onDeviceReady(){
                $(#应用程序状态-UL)追加('<立GT; deviceready事件接收到的LT; /李>');                document.addEventListener(后退按钮功能(E)
                {
                    $(#应用程序状态-UL)追加('<立GT;后退按钮的事件接收到的LT; /李>');                    如果($(#家)长度方式> 0)
                    {
                        //调用这个每次得到一个新的令牌。不要把它重用现有的令牌。
                        //pushNotification.unregister(successHandler,的ErrorHandler);
                        亦即preventDefault();
                        navigator.app.exitApp();
                    }
                    其他
                    {
                        navigator.app.backHistory();
                    }
                },FALSE);                尝试
                {
                    pushNotification = window.plugins.pushNotification;
              $(#应用程序状态-UL)追加('<立GT;注册'+ device.platform +'< /李>');
                    如果(device.platform =='机器人'|| device.platform =='Android的'||
                            device.platform =='亚马逊fireos'){
            pushNotification.register(successHandler,的ErrorHandler,{senderID:111111117746,欧洲央行:onNotification}); //需要!
                    }其他{
                        pushNotification.register(tokenHandler,的ErrorHandler,{徽章:真正的,声音:真,警告:真,欧洲央行:onNotificationAPN}); //需要!
                    }
                }
                赶上(错误)
                {
                    TXT =有此页面上的错误。\\ n \\ n;
                    TXT + =错误描述:+ err.message +\\ n \\ n;
                    警报(TXT);
                }
            }            //办理的iOS APNS通知
            功能onNotificationAPN(五){
                如果(e.alert){
                     $(#应用程序状态-UL)追加('<立GT;推通知:'+ e.alert +'< /李>');
                     //显示警报也需要org.apache.cordova.dialogs插件
                     navigator.notification.alert(e.alert);
                }                如果(e.sound){
                    //播放声音也需要org.apache.cordova.media插件
                    VAR SND =新媒体(e.sound);
                    snd.play();
                }                如果(e.badge){
                    pushNotification.setApplicationIconBadgeNumber(successHandler,e.badge);
                }
            }            //手柄GCM通知为Android
            功能onNotification(五){
                $(#应用程序状态-UL)追加('<立GT;活动 - GT&; RECEIVED:'+ e.event +'< /李>');                开关(e.event)
                {
                    情况下注册:
                    如果(e.regid.length大于0)
                    {
                        $(#应用程序状态-UL)追加('<立GT;注册 - > REGID:'+ e.regid +< /李>);
                        //你的GCM推送服务器需要知道REGID,才可以推动这个装置
                        //这里是你可能想给它发送REGID供以后使用。
                        的console.log(REGID =+ e.regid);
                    }
                    打破;                    案消息:
                        //如果设置此标志,这个通知发生,而我们是在前台。
                        //你可能想播放声音,以获得用户的关注,扔了一个对话框,等等。
                        如果(e.foreground)
                        {
                            $(#应用程序状态-UL)追加('<立GT; - INLINE NOTIFICATION--'+'< /李>');                                //在Android soundname是有效载荷之外。
                                    //在亚马逊FireOS所有自定义属性都包含有效载荷内
                                    VAR音效档= e.soundname || e.payload.sound;
                                    //如果通知包含soundname,播放。
                                    //播放声音也需要org.apache.cordova.media插件
                                    VAR my_media =新媒体(/ android_asset /网络/+音效档);                            my_media.play();
                        }
                        其他
                        {//否则,我们推出了因为用户感动在通知栏中的通知。
                            如果(e.coldstart)
                                $(#应用程序状态-UL)追加('<立GT; - COLDSTART NOTIFICATION--'+'< /李>');
                            其他
                            $(#应用程序状态-UL)追加('<立GT; - 背景NOTIFICATION--'+'< /李>');
                        }                        $(#应用程序状态-UL)追加('<李>消息 - >味精:'+ e.payload.message +'< /李>');
                        只有//机器人
                        $(#应用程序状态-UL)追加('<李>消息 - > MSGCNT:'+ e.payload.msgcnt +'< /李>');
                        只有//亚马逊fireos
                        $(#应用程序状态-UL)追加('<李>消息 - > TIMESTAMP:'+ e.payload.timeStamp +'< /李>');
                    打破;                    案错误:
                        $(#应用程序状态-UL)追加('<立GT;错误 - >味精:'+ e.msg +'< /李>');
                    打破;                    默认:
                        $(#应用程序状态-UL)追加('<李>活动 - >未知,收到一个事件,我们不知道它为< /李>');
                    打破;
                }
            }            功能tokenHandler(结果){
                $(#应用程序状态-UL)追加('<立GT;令牌:'+结果+'< /李>');
                //你的iOS推送服务器需要知道令牌才可以推动这个装置
                //这里是你可能想给它发送令牌以备后用。
            }            功能successHandler(结果){
                $(#应用程序状态-UL)追加('<立GT;成功:'+结果+'< /李>');
            }            功能的ErrorHandler(错误){
                $(#应用程序状态-UL)追加('<立GT;错误:'+误差+'< /李>');
            }            document.addEventListener(deviceready',onDeviceReady,真正的);         < / SCRIPT>
        < D​​IV ID =家>
            < D​​IV ID =应用程序状态-DIV>
                < UL ID =应用程序状态-UL>
                    <立GT;科尔多瓦PushNotification插件演示< /李>
                < / UL>
            < / DIV>
        < / DIV>
    < /身体GT;
< / HTML>


解决方案

找到了解决这个自己。事实证明,这两个JS文件:

 <脚本类型=文/ JavaScript的字符集=utf-8SRC =cordova.js>< / SCRIPT>
 <脚本类型=文/ JavaScript的SRC =PushNotification.js>< / SCRIPT>

不得不在HEAD部分被移动,而不是体内部分。然后,它工作正常!

I used the example source code from (https://github.com/phonegap-build/PushPlugin/tree/master/Example/www). Problem is that, when I install my app and launch it my android phone (or android SDK emulator), it fails to register device to Android GCM. All the time error pops-up - "Device is not defined". I dont know exactly how I can catch error, why it fails to register device.

Steps that I preformed is:

  1. – Created Phonegap project

  2. – Installed following 3 plugins (cordova-plugin-device.git, cordova-plugin-media.git, PushPlugin).

  3. – Registered App in Google Android GCM and retrieved my Goolge cloud API and Project Number
  4. Locate the PushNotification.js file that was installed into my project-root/plugins folder. This file was copied into my project-root/www folder and referenced from the index.html
  5. Added in my index file
  6. Added to my www/config.xml file
  7. Added JQuery mobile JS + CSS files to my project
  8. Changed senderID in my HTML code:"1111146417746
  9. Builded app with CLI - phonegap build android

And here is my Index.html code:

<html>
    <head>
        <title>com.PhoneGap.c2dm</title>
    </head>
    <body>


        <!--<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>-->
     <link rel="stylesheet" href="ext/jquery/jquery.mobile-min.css">
    <script type="text/javascript" src="ext/jquery/jquery-min.js"></script>
    <script type="text/javascript" src="ext/jquery/jquery.mobile-min.js"></script>
    <script type="text/javascript" src="ext/jquery.qrcode.js"></script>
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" src="PushNotification.js"></script>

        <script type="text/javascript">
            var pushNotification;

            function onDeviceReady() {
                $("#app-status-ul").append('<li>deviceready event received</li>');

                document.addEventListener("backbutton", function(e)
                {
                    $("#app-status-ul").append('<li>backbutton event received</li>');

                    if( $("#home").length > 0)
                    {
                        // call this to get a new token each time. don't call it to reuse existing token.
                        //pushNotification.unregister(successHandler, errorHandler);
                        e.preventDefault();
                        navigator.app.exitApp();
                    }
                    else
                    {
                        navigator.app.backHistory();
                    }
                }, false);

                try 
                { 
                    pushNotification = window.plugins.pushNotification;
              $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
                    if (device.platform == 'android' || device.platform == 'Android' ||
                            device.platform == 'amazon-fireos' ) {
            pushNotification.register(successHandler, errorHandler, {"senderID":"111111117746","ecb":"onNotification"});        // required!
                    } else {
                        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); 
                } 
            }

            // handle APNS notifications for iOS
            function onNotificationAPN(e) {
                if (e.alert) {
                     $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
                     // showing an alert also requires the org.apache.cordova.dialogs plugin
                     navigator.notification.alert(e.alert);
                }

                if (e.sound) {
                    // playing a sound also requires the org.apache.cordova.media plugin
                    var snd = new Media(e.sound);
                    snd.play();
                }

                if (e.badge) {
                    pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
                }
            }

            // handle GCM notifications for Android
            function onNotification(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>');

                                // on Android soundname is outside the payload. 
                                    // On Amazon FireOS all custom attributes are contained within payload
                                    var soundfile = e.soundname || e.payload.sound;
                                    // if the notification contains a soundname, play it.
                                    // playing a sound also requires the org.apache.cordova.media plugin
                                    var my_media = new Media("/android_asset/www/"+ soundfile);

                            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>');
                        //android only
                        $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                        //amazon-fireos only
                        $("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</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>');
            }

            document.addEventListener('deviceready', onDeviceReady, true);

         </script>
        <div id="home">
            <div id="app-status-div">
                <ul id="app-status-ul">
                    <li>Cordova PushNotification Plugin Demo</li>
                </ul>
            </div>
        </div>
    </body>
</html>

解决方案

Found a solution for this myself. As it turned out, these two JS files:

 <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
 <script type="text/javascript" src="PushNotification.js"></script>

had to been moved in HEAD section, not inside BODY section. Then it works fine!

这篇关于我发动PhoneGap的推送通知的应用程序时,不定义设备错误弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆