PhoneGap 3.1 Build Device Is Not Defined

2020-06-24 06:55发布

问题:

I have a sencha touch project. I was build with phonegap 2.9 and works fine device.uuid returning to device Id. When I tried the build with 3.1 device.uuid throwing " Device Is Not Defined" error. My config.xml

<preference name="phonegap-version" value="3.1.0" />
<preference name="stay-in-webview" value="true" />
<access origin="*" />

<gap:plugin name="com.phonegap.plugins.barcodescanner" />
<gap:splash src="images/splash.png" />

<feature name="http://api.phonegap.com/1.0/camera" />

<feature name="Device">
  <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>

My Device ID request:

 try
{
   Ext.getCmp('txtUUID').setValue(device.uuid);
}
catch(err)
{alert(err);

    Ext.getCmp('txtUUID').setValue('ID Okunamadı!');

}

I need help. How to resolve this problem ?

回答1:

I assume you are building with phonegap build, right?

In phonegap 3, the core api are no more included by default and are now plugins, so you have to explicitly add any api you are using.

You should be able to read the UUID after adding the following line to config.xml :

<gap:plugin name="org.apache.cordova.device" />

And you can also remove the following lines since the API features are now handled by the new plugin format.

useless lines:

<feature name="http://api.phonegap.com/1.0/camera" />
<feature name="Device">
  <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>


回答2:

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); 
            } 
        }