最后两天后我解决了上述问题:
试试下面的命令。
$ ionic cordova platform rm android
$ ionic cordova platform rm ios
$ ionic cordova plugin add https://github.com/tushe/cordova-plugin-background-mode.git
$ npm install --save @ionic-native/background-mode
$ ionic cordova platform add android
$ ionic cordova platform add ios
如果 background-mode 不工作设置以下 cordova-plugin-background-mode 中的更改对我有用。崩溃问题已解决,后台插件工作正常。
在 ForegroundService.java 中进行了以下更改:
Add below import statement: import android.app.NotificationChannel;
b) 添加以下全局变量:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";
c) 用以下代码替换 keepAwake() 方法:
private void keepAwake() {
JSONObject settings = BackgroundMode.getSettings();
boolean isSilent = settings.optBoolean("silent", false);
if (!isSilent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
} else {
startForeground(NOTIFICATION_ID, makeNotification());
}
}
PowerManager powerMgr = (PowerManager)
getSystemService(POWER_SERVICE);
wakeLock = powerMgr.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
wakeLock.acquire();
}
在 AndroidManifest.xml 文件中添加以下内容:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
在我调用后台模式插件的代码中,在激活时使用了 disableWebViewOptimizations 选项:
cordova.plugins.backgroundMode.on('activate', function() {
cordova.plugins.backgroundMode.disableWebViewOptimizations();
});
以上程序对我有用。