# HG changeset patch # User Carl Byington # Date 1602712135 25200 # Node ID 7545103ec815826617a057477bb3ffcc24d4f2a9 # Parent b257517e9bef5dce6cf4695fd77381f72e922133 use foreground service and notification channel on Android 8+ diff -r b257517e9bef -r 7545103ec815 app/build.gradle --- a/app/build.gradle Wed Oct 14 12:26:53 2020 -0700 +++ b/app/build.gradle Wed Oct 14 14:48:55 2020 -0700 @@ -60,7 +60,7 @@ defaultConfig { applicationId = "com.five_ten_sg.connectbot" - minSdkVersion = 26 + minSdkVersion = 21 targetSdkVersion = 28 ndk { moduleName = "com_google_ase_Exec" diff -r b257517e9bef -r 7545103ec815 app/src/main/AndroidManifest.xml --- a/app/src/main/AndroidManifest.xml Wed Oct 14 12:26:53 2020 -0700 +++ b/app/src/main/AndroidManifest.xml Wed Oct 14 14:48:55 2020 -0700 @@ -17,8 +17,8 @@ --> diff -r b257517e9bef -r 7545103ec815 app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java --- a/app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java Wed Oct 14 12:26:53 2020 -0700 +++ b/app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java Wed Oct 14 14:48:55 2020 -0700 @@ -31,6 +31,7 @@ import android.content.res.Resources; import android.graphics.Color; import android.graphics.BitmapFactory; +import android.os.Build; /** * @author Kenny Root @@ -60,25 +61,31 @@ Intent notificationIntent = new Intent(context, ConsoleActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); - String channelID = "com.five_ten_sg.connectbot.service"; - String channelName = "background"; - if (chan == null) { - chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); - chan.setLightColor(R.color.colorAccent); - chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); - NotificationManager manager = getNotificationManager(context); - assert manager != null; - manager.createNotificationChannel(chan); + Notification.Builder nb; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + String channelID = "com.five_ten_sg.connectbot.service"; + String channelName = "background"; + if (chan == null) { + chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); + chan.setLightColor(R.color.colorAccent); + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + NotificationManager manager = getNotificationManager(context); + assert manager != null; + manager.createNotificationChannel(chan); + } + nb = new Notification.Builder(context, channelID); + } + else { + nb = new Notification.Builder(context); } - Notification notification = new Notification.Builder(context) + Notification notification = nb .setContentTitle(res.getString(R.string.app_name)) .setContentText(res.getString(R.string.notification_text, host.getNickname())) .setContentIntent(contentIntent) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.notification_icon) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) - .setChannelId(channelID) .setLights(rgb, 300, 1000) .build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; @@ -90,18 +97,25 @@ Intent notificationIntent = new Intent(context, ConsoleActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); - String channelID = "com.five_ten_sg.connectbot.service"; - String channelName = "background"; - if (chan == null) { - chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); - chan.setLightColor(R.color.colorAccent); - chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); - NotificationManager manager = getNotificationManager(context); - assert manager != null; - manager.createNotificationChannel(chan); + Notification.Builder nb; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + String channelID = "com.five_ten_sg.connectbot.service"; + String channelName = "background"; + if (chan == null) { + chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); + chan.setLightColor(R.color.colorAccent); + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + NotificationManager manager = getNotificationManager(context); + assert manager != null; + manager.createNotificationChannel(chan); + } + nb = new Notification.Builder(context, channelID); + } + else { + nb = new Notification.Builder(context); } - Notification notification = new Notification.Builder(context) + Notification notification = nb .setContentTitle(res.getString(R.string.app_name)) .setContentText(res.getString(R.string.app_is_running)) .setContentIntent(contentIntent) @@ -109,7 +123,6 @@ .setOngoing(true) .setSmallIcon(R.drawable.notification_icon) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) - .setChannelId(channelID) .build(); notification.flags |= Notification.FLAG_NO_CLEAR; return notification;