Really fix notifications on Oreo

This commit is contained in:
Tad 2018-04-05 21:46:24 -04:00
parent 0eb386f8ce
commit 23e76dd8cf
4 changed files with 20 additions and 8 deletions

View file

@ -6,8 +6,8 @@ android {
applicationId "us.spotco.malwarescanner"
minSdkVersion 16
targetSdkVersion 26
versionCode 30
versionName "2.5"
versionCode 31
versionName "2.6"
}
buildTypes {
debug {

View file

@ -2,8 +2,10 @@ package us.spotco.malwarescanner;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
@ -40,6 +42,11 @@ class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
logOutput = activity.findViewById(R.id.txtLogOutput);
} else {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel detectionChannel = new NotificationChannel("DETECTION", context.getString(R.string.lblNotificationMalwareDetectionTitle), NotificationManager.IMPORTANCE_HIGH);
detectionChannel.setDescription(context.getString(R.string.lblNotificationMalwareDetectionDescription));
notificationManager.createNotificationChannel(detectionChannel);
}
}
}
@ -58,6 +65,9 @@ class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setVisibility(Notification.VISIBILITY_SECRET);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId("DETECTION");
}
notificationManager.notify(new Random().nextInt(), mBuilder.build());
//Log.d("Theia", result);
}

View file

@ -52,6 +52,12 @@ public class MalwareScannerService extends Service {
}
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
foregroundChannel.setShowBadge(false);
notificationManager.createNotificationChannel(foregroundChannel);
}
setForeground();
return START_STICKY;
}
@ -96,13 +102,7 @@ public class MalwareScannerService extends Service {
.setShowWhen(false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setShowBadge(false);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
notificationManager.createNotificationChannel(foregroundChannel);
foregroundNotification.setChannelId("FOREGROUND");
//Importance seems to be getting ignored
//Users will have to manually set it to 'Low' in order to hide it from the lock screen and status bar
}
startForeground(-1, foregroundNotification.build());

View file

@ -7,6 +7,8 @@
<string name="lblScanApps">Scan App APKs</string>
<string name="lblScanInternal">Scan Internal Storage</string>
<string name="lblScanExternal">Scan External Storage</string>
<string name="lblNotificationMalwareDetectionTitle">Malware Detection</string>
<string name="lblNotificationMalwareDetectionDescription">Used to alert when malware is detected</string>
<string name="lblNotificationRealtimeTitle">Realtime Scanner</string>
<string name="lblNotificationRealtimeDescription">Used to show files scanned counter and maintain the background service</string>
<string name="lblNotificationRealtimeText">Malware will be detected in realtime</string>