|
1 | 1 | package org.droidplanner.services.android.impl.api;
|
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint;
|
| 4 | +import android.annotation.TargetApi; |
4 | 5 | import android.app.Notification;
|
| 6 | +import android.app.NotificationChannel; |
| 7 | +import android.app.NotificationManager; |
5 | 8 | import android.app.Service;
|
6 | 9 | import android.content.ComponentName;
|
7 | 10 | import android.content.Context;
|
8 | 11 | import android.content.Intent;
|
9 | 12 | import android.content.pm.PackageManager;
|
| 13 | +import android.os.Build; |
10 | 14 | import android.os.Handler;
|
11 | 15 | import android.os.IBinder;
|
12 | 16 | import android.os.Looper;
|
@@ -219,21 +223,47 @@ public void onCreate() {
|
219 | 223 | private void updateForegroundNotification() {
|
220 | 224 | final Context context = getApplicationContext();
|
221 | 225 |
|
222 |
| - //Put the service in the foreground |
223 |
| - final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) |
224 |
| - .setContentTitle("DroneKit-Android") |
225 |
| - .setPriority(NotificationCompat.PRIORITY_MIN) |
226 |
| - .setSmallIcon(R.drawable.ic_stat_notify); |
| 226 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 227 | + startMyOwnForeground(); |
| 228 | + } |
| 229 | + else { |
| 230 | + //Put the service in the foreground |
| 231 | + final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) |
| 232 | + .setContentTitle("DroneKit-Android") |
| 233 | + .setPriority(NotificationCompat.PRIORITY_MIN) |
| 234 | + .setSmallIcon(R.drawable.ic_stat_notify); |
| 235 | + |
| 236 | + final int connectedCount = droneApiStore.size(); |
| 237 | + if (connectedCount > 1) { |
| 238 | + notifBuilder.setContentText(connectedCount + " connected apps"); |
| 239 | + } |
227 | 240 |
|
228 |
| - final int connectedCount = droneApiStore.size(); |
229 |
| - if (connectedCount > 1) { |
230 |
| - notifBuilder.setContentText(connectedCount + " connected apps"); |
| 241 | + final Notification notification = notifBuilder.build(); |
| 242 | + startForeground(FOREGROUND_ID, notification); |
231 | 243 | }
|
| 244 | + } |
232 | 245 |
|
233 |
| - final Notification notification = notifBuilder.build(); |
| 246 | + @TargetApi(Build.VERSION_CODES.O) |
| 247 | + private void startMyOwnForeground(){ |
| 248 | + String NOTIFICATION_CHANNEL_ID = "DroneKit-Android"; |
| 249 | + String channelName = "DroidPlanner Service"; |
| 250 | + NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); |
| 251 | + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
| 252 | + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 253 | + assert manager != null; |
| 254 | + manager.createNotificationChannel(chan); |
| 255 | + |
| 256 | + Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID); |
| 257 | + Notification notification = notificationBuilder.setOngoing(true) |
| 258 | + .setSmallIcon(R.drawable.ic_stat_notify) |
| 259 | + .setContentTitle("DroneKit-Android") |
| 260 | + .setPriority(Notification.PRIORITY_MIN) |
| 261 | + .setCategory(Notification.CATEGORY_SERVICE) |
| 262 | + .build(); |
234 | 263 | startForeground(FOREGROUND_ID, notification);
|
235 | 264 | }
|
236 | 265 |
|
| 266 | + |
237 | 267 | @Override
|
238 | 268 | public void onDestroy() {
|
239 | 269 | super.onDestroy();
|
|
0 commit comments