Mercurial > 510Connectbot
diff app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 490:7545103ec815 stable-1.9.4-2
use foreground service and notification channel on Android 8+
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 14 Oct 2020 14:48:55 -0700 |
parents | 869070df0e80 |
children | cbdb219e9ff5 |
line wrap: on
line diff
--- 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;