调用插件闪退是因为backemode插件的问题,并且这个问题仅在android 8.1上出现。经过一番查阅后发现,在android8.1之后我们必须创建自己的通知通道。修改cordova-plugin-background-mode/src/android/ForegroundService.java文件
找到下面代码
Notification.Builder notification=newNotification.Builder(context).setContentTitle(title).setContentText(text).setOngoing(true).setSmallIcon(getIconResId(settings));
在之后添加:
//upgrade to sdk 26, fix problems with android 8.1
if (android.os.Build.VERSION.SDK_INT >= 26) {
//Set the channel’s ID 这里要填写你自己的app name//
String CHANNEL_ONE_ID = "com.yourapp.ONE";
//Set the channel’s user-visible name//
String CHANNEL_ONE_NAME = "Channel One";
//This only needs to be run on Devices on Android O and above
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.setLightColor(Color.MAGENTA);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
notification.setChannelId(CHANNEL_ONE_ID);
}
if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN);
}