我正在使用 Cordova / Phonegap iBeacon plugin与 ionicframework在我的 Cordova 项目中。我正在尝试使用 cordova local notification plugin 在 android 和 ios 上发送本地通知进入监控区域时,当应用程序被杀死时。
这是我的代码:
document.addEventListener("deviceready", onDeviceReady, false);
function didDetermineStateForRegion(pluginResult) {
}
function didStartMonitoringForRegion (pluginResult) {
}
function didExitRegion(pluginResult) {
$cordovaLocalNotification.add({
id: 30244234234,
title: "Good By!",
text: "Hope to see you again."
}).then(function () {
});
}
function didEnterRegion (pluginResult) {
$cordovaLocalNotification.add({
title: "Welcome",
text: "Tap to launch app"
}).then(function () {
});
};
function didRangeBeaconsInRegion (pluginResult) {
}
function onDeviceReady() {
// Now safe to use device APIs
function createBeacon(uuid,nofiyState) {
var uuid = uuid; // mandatory
var identifier = 'estimote'; // mandatory
// throws an error if the parameters are not valid
var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid);
beaconRegion.notifyEntryStateOnDisplay = true;
return beaconRegion;
}
var delegate = new cordova.plugins.locationManager.Delegate();
delegate.didDetermineStateForRegion = didDetermineStateForRegion;
delegate.didStartMonitoringForRegion = didStartMonitoringForRegion;
delegate.didRangeBeaconsInRegion = didRangeBeaconsInRegion;
delegate.didEnterRegion = didEnterRegion;
delegate.didExitRegion = didExitRegion;
var beaconRegion = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',true);
// var beaconRegionBlue = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',1,,true);
cordova.plugins.locationManager.setDelegate(delegate);
// required in iOS 8+
//cordova.plugins.locationManager.requestWhenInUseAuthorization();
cordova.plugins.locationManager.requestAlwaysAuthorization();
cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion)
.fail(console.error)
.done();
}
Cordova 插件:
com.unarin.cordova.beacon 3.3.0 "Proximity Beacon Plugin"
de.appplant.cordova.plugin.local-notification 0.8.1 "LocalNotification"
nl.x-services.plugins.socialsharing 4.3.16 "SocialSharing"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"
Cordova 版本:4.3.0
即使应用程序被杀死,这对 ios 也能正常工作,但在 android 上,只有当应用程序在后台时才会出现通知。当我从 Android 上的任务管理器中终止应用程序时,我从未看到任何本地通知。
即使应用程序被杀死,是否有可能在 Android 上收到通知?
感谢您的帮助。
最佳答案
让我们清除一些东西,有些状态让你感到困惑:
- 应用即服务
- 应用在后台运行(即最小化)。
- 应用被杀死(根本没有运行)
在所有情况下,当您终止应用程序时的第三种状态(通过在自定义 rom 中长按后退按钮,或从操作系统中的应用程序菜单强制停止),应用程序只是从内存中删除,没有代码被执行!
在这种情况下通常做的是在服务停止后自动重新启动服务检查这个 answer ,如您所见:
it is really very bad pattern to run service forcefully against the user's willingness.
有很多 cordova 插件可以创建 BroadcasteReceiver
,但是您的问题的简单答案是,如果应用程序被终止,则无法接收通知。
但是您应该考虑这一点:如果用户终止了您的应用程序,则意味着这是故意的,因此您不必担心您的应用程序是否可以运行,因为这是用户的问题,而不是您作为开发人员的问题。
关于javascript - Cordova 山羊;应用程序被杀死后发送本地通知,但在 android 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29940742/