diff options
author | Joey Grover <joeygrover@gmail.com> | 2020-02-11 12:04:29 -0500 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2020-02-11 12:04:29 -0500 |
commit | 870a118d8167270841e219ed843e9dc65bb16d62 (patch) | |
tree | 6b151f1e8da1b5504f345a3025d637cd5696d55c | |
parent | 7e6a16c027bcdd0fb523a9993dc59b0171167aea (diff) | |
download | sdl_android-870a118d8167270841e219ed843e9dc65bb16d62.tar.gz |
Delete notification channel when possible
Added the notification channel delete method call back into the router service. Some phones like the pixel 2 & 3 were only clearing the notificaiton 5-7 seconds after the service called stopForeground. Deleting the channel removes the notification instantly. We added a check to avoid a race condition in which the channel is deleted before the notifcation was actually shown.
-rw-r--r-- | android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index 3abd61094..32cbeae10 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -69,6 +69,7 @@ import android.os.Parcel; import android.os.ParcelFileDescriptor; import android.os.Parcelable; import android.os.RemoteException; +import android.service.notification.StatusBarNotification; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -1516,6 +1517,27 @@ public class SdlRouterService extends Service{ synchronized (NOTIFICATION_LOCK) { if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport this.stopForeground(true); + NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager!= null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + try { + boolean notificationHasDisplayed = false; + StatusBarNotification[] notifications = notificationManager.getActiveNotifications(); + for (StatusBarNotification notification : notifications) { + if(notification != null && FOREGROUND_SERVICE_ID == notification.getId()){ + DebugTool.logInfo("Service notification is being displayed"); + notificationHasDisplayed = true; + break; + } + } + if (notificationHasDisplayed) { + notificationManager.deleteNotificationChannel(SDL_NOTIFICATION_CHANNEL_ID); + } + //else leave the notification channel alone to avoid deleting it before the + //foreground service notification has a chance to be displayed. + } catch (Exception e){ + DebugTool.logError("Issue when deleting notification channel", e); + } + } isForeground = false; } } |