我正在尝试在我的一个Ionic-3应用程序中使用地理围栏 . 按照https://ionicframework.com/docs/native/geofence/中的说明操作后,我修改了我的app.components.ts,现在看起来像这样:
import { Geofence } from '@ionic-native/geofence';
@Component({ templateUrl: 'app.html' }) export class MyApp {
constructor(private geofence: Geofence) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.startGeofence();
});
this.pages = [
]; }
startGeofence() {
// initialize the plugin
this.geofence.initialize().then(
// resolved promise does not return a value
() => {
console.log('Geofence Plugin Ready');
this.addGeofence();
},
(err) => {
console.log('Geofence Plugin failed');
console.log(err);
}
) }
private addGeofence() {
this.geofence.removeAll();
//options describing geofence
let fence = {
id: '69ca1b88-6fbe-4e80-a4d4-ff4d3748acab', //any unique ID
latitude: 41.2713354, //center of geofence radius
longitude: 18.6108311,
radius: 20, //radius to edge of geofence in meters
transitionType: 3, //see 'Transition Types' below
notification: { //notification settings
id: 3, //any unique ID
title: 'Geofencing', //notification title
text: 'my home', //notification body
openAppOnClick: true //open app when notification is tapped
}
}
this.geofence.addOrUpdate(fence).then(
() => console.log('Geofence added'),
(err) => console.log('Geofence failed to add')
);
this.geofence.onTransitionReceived().subscribe(resp => {
resp.forEach(function (geo) {
console.log("Geofence transition detected", geo);
});
console.log("geofence on transition recieved", resp);
alert(JSON.stringify(resp));
}); } }
在控制台日志中,我有以下消息:
deviceready在5秒后没有发射 . Ionic Native:4964 ms之后触发的deviceready事件Geofence Plugin Ready Geofence添加
那些显示插件的正确初始化,但是,之后没有任何反应,从不调用onTransitionReceived . 我使用的是Android 6.0智能手机 .
我的package.json:
{ "name": "ClientIonicProject", "version": "0.0.1", "author":
"Ionic Framework", "homepage": "http://ionicframework.com/",
"private": true, "scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"lint": "ionic-app-scripts lint",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve" }, "dependencies": {
"@angular/common": "^5.0.1",
"@angular/compiler": "^5.0.1",
"@angular/compiler-cli": "^5.0.1",
"@angular/core": "^5.0.1",
"@angular/forms": "^5.0.1",
"@angular/http": "^5.0.1",
"@angular/platform-browser": "^5.0.1",
"@angular/platform-browser-dynamic": "^5.0.1",
"@ionic-native/app-version": "^4.4.0",
"@ionic-native/core": "4.3.3",
"@ionic-native/geofence": "^4.7.0",
"@ionic-native/geolocation": "^4.4.0",
"@ionic-native/http": "^4.4.0",
"@ionic-native/in-app-browser": "^4.4.0",
"@ionic-native/launch-navigator": "^4.4.0",
"@ionic-native/network": "^4.5.2",
"@ionic-native/splash-screen": "4.3.3",
"@ionic-native/status-bar": "4.3.3",
"@ionic-native/text-to-speech": "^4.4.0",
"@ionic/storage": "^2.1.3",
"@ngx-translate/core": "^8.0.0",
"@ngx-translate/http-loader": "^2.0.0",
"cordova-android": "6.3.0",
"cordova-plugin-actionsheet": "^2.3.3",
"cordova-plugin-add-swift-support": "^1.6.0",
"cordova-plugin-advanced-http": "^1.11.1",
"cordova-plugin-app-version": "^0.1.9",
"cordova-plugin-compat": "^1.2.0",
"cordova-plugin-device": "^1.1.4",
"cordova-plugin-dialogs": "^2.0.1",
"cordova-plugin-file": "^6.0.1",
"cordova-plugin-geofence": "^0.7.0",
"cordova-plugin-geolocation": "^3.0.0",
"cordova-plugin-inappbrowser": "^1.7.2",
"cordova-plugin-ionic": "^3.1.3",
"cordova-plugin-ionic-webview": "^1.2.1",
"cordova-plugin-network-information": "^1.3.4",
"cordova-plugin-splashscreen": "^5.0.2",
"cordova-plugin-statusbar": "^2.4.2",
"cordova-plugin-tts": "^0.2.3",
"cordova-plugin-whitelist": "^1.3.1",
"cordova-sqlite-storage": "^2.3.1",
"es6-promise-plugin": "^4.2.2",
"ionic-angular": "3.9.2",
"ionic-plugin-keyboard": "^2.2.1",
"ionicons": "3.0.0",
"rxjs": "5.5.2",
"sw-toolbox": "3.6.0",
"uk.co.workingedge.phonegap.plugin.launchnavigator": "^4.2.0",
"zone.js": "0.8.18" }, "devDependencies": {
"@ionic/app-scripts": "3.1.0",
"typescript": "2.4.2" }, "description": "An Ionic project", "cordova": {
"plugins": {
"cordova-plugin-tts": {},
"cordova-plugin-app-version": {},
"cordova-plugin-geolocation": {
"GEOLOCATION_USAGE_DESCRIPTION": "To locate you"
},
"cordova-plugin-inappbrowser": {},
"uk.co.workingedge.phonegap.plugin.launchnavigator": {},
"cordova-plugin-advanced-http": {},
"ionic-plugin-keyboard": {},
"cordova-plugin-whitelist": {},
"cordova-plugin-device": {},
"cordova-plugin-ionic-webview": {},
"cordova-plugin-network-information": {},
"cordova-sqlite-storage": {},
"cordova-plugin-geofence": {},
"cordova-plugin-add-swift-support": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-splashscreen": {}
},
"platforms": [
"android"
] } }
离子信息:
cli packages: (C:\Users\francesco\AppData\Roaming\npm\node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 8.0.0
local packages:
@ionic/app-scripts : 3.1.0
Cordova Platforms : android 6.3.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v6.10.3
npm : 6.0.0
OS : Windows 10
Environment Variables:
ANDROID_HOME : D:\e\programmi_esterni\Android\android-sdk
Misc:
backend : legacy
建议?