【发布时间】:2018-04-25 19:24:11
【问题描述】:
场景
- 我们的应用程序旨在检测放置在我们应用程序餐厅内的信标 使用 react-native-beacons-manager
- 当我们的应用检测到信标时,我开发了一个云功能,它接受信标的主键并使用它从我的数据库中查询该餐厅的数据
- 云功能随后会向用户发送有关餐厅详细信息的推送通知。
问题
我检测信标的方式并不稳定。这是我的流程。我创建了一个位于
的函数this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
//function-here
);
我可以接收信标信息,例如 uuid、主要和次要键以及邻近度(立即、近、远、未知)。现在在该函数中,我使用主键来确定每个信标的个性。现在,我做了一个这样的条件:
let beaconArr = data.beacons;
console.log(beaconArr);
console.log(count);
if (beaconArr.length > 0) {
console.log("beacons detected!");
let major = data.beacons[0].major;
let prox = data.beacons[0].proximity;
if ((prox === "near" || prox === "far") && beaconFlag === false && count === 0) {
console.log("beacon Action");
this.props.beaconAction(major);
this.props.createCheckInHistory(user.uid);
beaconFlag = true;
count++;
} else {
console.log("counter turned to 1!");
console.log(data);
beaconFlag = true;
}
} else {
console.log("no beacons detected!");
count = 0;
beaconFlag = false;
}
预期结果
我希望条件为 true 的函数只会触发一次。
实际结果 有时,它可以,有时它不是。即使我还在信标的范围内,突然信标的数组变成了0。然后突然我会一次又一次地收到推送通知。
componentDidMount() 代码
componentDidMount() {
this.props.selectedIcon('map');
firebase
.messaging()
.getInitialNotification()
.then(notification => {
console.log("Notification which opened the app: ", notification);
});
const user = firebase.auth().currentUser;
let count = 0;
let beaconFlag = false;
// will be set as a reference to "regionDidEnter" event:
this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
"beaconsDidRange",
_.throttle(data => {
let beaconArr = data.beacons;
console.log(beaconArr);
console.log(count);
if (beaconArr.length > 0) {
console.log("beacons detected!");
let major = data.beacons[0].major;
let prox = data.beacons[0].proximity;
if ((prox === "near" || prox === "far") && beaconFlag === false && count === 0) {
console.log("beacon Action");
this.props.beaconAction(major);
this.props.createCheckInHistory(user.uid);
beaconFlag = true;
count++;
} else {
console.log("counter turned to 1!");
console.log(data);
beaconFlag = true;
}
} else {
console.log("no beacons detected!");
count = 0;
beaconFlag = false;
}
}, 3000)
);
// monitoring events
this.regionDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
"regionDidEnter",
data => {
console.log("monitoring - regionDidEnter data: ", data);
}
);
// Monitoring: Listen for device leaving the defined region
this.regionDidExitEvent = Beacons.BeaconsEventEmitter.addListener(
"regionDidExit",
data => {
console.log("monitoring - regionDidExit data: ", data);
}
);
}
【问题讨论】:
标签: react-native ibeacon