Mercurial > 510Connectbot
comparison 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 |
comparison
equal
deleted
inserted
replaced
489:b257517e9bef | 490:7545103ec815 |
---|---|
29 import android.content.Context; | 29 import android.content.Context; |
30 import android.content.Intent; | 30 import android.content.Intent; |
31 import android.content.res.Resources; | 31 import android.content.res.Resources; |
32 import android.graphics.Color; | 32 import android.graphics.Color; |
33 import android.graphics.BitmapFactory; | 33 import android.graphics.BitmapFactory; |
34 import android.os.Build; | |
34 | 35 |
35 /** | 36 /** |
36 * @author Kenny Root | 37 * @author Kenny Root |
37 * | 38 * |
38 * Based on the concept from jasta's blog post. | 39 * Based on the concept from jasta's blog post. |
58 rgb = Color.WHITE; | 59 rgb = Color.WHITE; |
59 Resources res = context.getResources(); | 60 Resources res = context.getResources(); |
60 Intent notificationIntent = new Intent(context, ConsoleActivity.class); | 61 Intent notificationIntent = new Intent(context, ConsoleActivity.class); |
61 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); | 62 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); |
62 | 63 |
63 String channelID = "com.five_ten_sg.connectbot.service"; | 64 Notification.Builder nb; |
64 String channelName = "background"; | 65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
65 if (chan == null) { | 66 String channelID = "com.five_ten_sg.connectbot.service"; |
66 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); | 67 String channelName = "background"; |
67 chan.setLightColor(R.color.colorAccent); | 68 if (chan == null) { |
68 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); | 69 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); |
69 NotificationManager manager = getNotificationManager(context); | 70 chan.setLightColor(R.color.colorAccent); |
70 assert manager != null; | 71 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
71 manager.createNotificationChannel(chan); | 72 NotificationManager manager = getNotificationManager(context); |
73 assert manager != null; | |
74 manager.createNotificationChannel(chan); | |
75 } | |
76 nb = new Notification.Builder(context, channelID); | |
77 } | |
78 else { | |
79 nb = new Notification.Builder(context); | |
72 } | 80 } |
73 | 81 |
74 Notification notification = new Notification.Builder(context) | 82 Notification notification = nb |
75 .setContentTitle(res.getString(R.string.app_name)) | 83 .setContentTitle(res.getString(R.string.app_name)) |
76 .setContentText(res.getString(R.string.notification_text, host.getNickname())) | 84 .setContentText(res.getString(R.string.notification_text, host.getNickname())) |
77 .setContentIntent(contentIntent) | 85 .setContentIntent(contentIntent) |
78 .setWhen(System.currentTimeMillis()) | 86 .setWhen(System.currentTimeMillis()) |
79 .setSmallIcon(R.drawable.notification_icon) | 87 .setSmallIcon(R.drawable.notification_icon) |
80 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) | 88 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) |
81 .setChannelId(channelID) | |
82 .setLights(rgb, 300, 1000) | 89 .setLights(rgb, 300, 1000) |
83 .build(); | 90 .build(); |
84 notification.flags |= Notification.FLAG_AUTO_CANCEL; | 91 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
85 return notification; | 92 return notification; |
86 } | 93 } |
88 protected Notification newRunningNotification(Context context) { | 95 protected Notification newRunningNotification(Context context) { |
89 Resources res = context.getResources(); | 96 Resources res = context.getResources(); |
90 Intent notificationIntent = new Intent(context, ConsoleActivity.class); | 97 Intent notificationIntent = new Intent(context, ConsoleActivity.class); |
91 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); | 98 PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, 0); |
92 | 99 |
93 String channelID = "com.five_ten_sg.connectbot.service"; | 100 Notification.Builder nb; |
94 String channelName = "background"; | 101 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
95 if (chan == null) { | 102 String channelID = "com.five_ten_sg.connectbot.service"; |
96 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); | 103 String channelName = "background"; |
97 chan.setLightColor(R.color.colorAccent); | 104 if (chan == null) { |
98 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); | 105 chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE); |
99 NotificationManager manager = getNotificationManager(context); | 106 chan.setLightColor(R.color.colorAccent); |
100 assert manager != null; | 107 chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
101 manager.createNotificationChannel(chan); | 108 NotificationManager manager = getNotificationManager(context); |
109 assert manager != null; | |
110 manager.createNotificationChannel(chan); | |
111 } | |
112 nb = new Notification.Builder(context, channelID); | |
113 } | |
114 else { | |
115 nb = new Notification.Builder(context); | |
102 } | 116 } |
103 | 117 |
104 Notification notification = new Notification.Builder(context) | 118 Notification notification = nb |
105 .setContentTitle(res.getString(R.string.app_name)) | 119 .setContentTitle(res.getString(R.string.app_name)) |
106 .setContentText(res.getString(R.string.app_is_running)) | 120 .setContentText(res.getString(R.string.app_is_running)) |
107 .setContentIntent(contentIntent) | 121 .setContentIntent(contentIntent) |
108 .setWhen(0) | 122 .setWhen(0) |
109 .setOngoing(true) | 123 .setOngoing(true) |
110 .setSmallIcon(R.drawable.notification_icon) | 124 .setSmallIcon(R.drawable.notification_icon) |
111 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) | 125 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon)) |
112 .setChannelId(channelID) | |
113 .build(); | 126 .build(); |
114 notification.flags |= Notification.FLAG_NO_CLEAR; | 127 notification.flags |= Notification.FLAG_NO_CLEAR; |
115 return notification; | 128 return notification; |
116 | 129 |
117 } | 130 } |