Device is not defined error pops up when launching my Phonegap Push notification app

核能气质少年 提交于 2019-12-04 13:57:44
woodroxx

Go to your plugins folder and check whether you have a folder named cordova-plugin-device. That plugin should be installed in your project. So if that folder is missing you need to install it by opening your terminal and going to the root of your project to execute the following command

cordova plugin add cordova-plugin-device

This should add the plugin.

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!

Put your registration code under DeviceReady function

var platform = null;
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
          platform = device.platform;
          //alert(platform);
          $("#app-status-ul").append('<li>'+ platform +'</li>');
          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":"860557673192","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); 
            } 
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!