@@ -1344,6 +1344,7 @@ private int getNotificationTimeout(String address){
13441344 // If this is the first time the service has ever connected to this device we want
13451345 // to ensure we have a record of it
13461346 setSDLConnectedStatus (address , false );
1347+ return FOREGROUND_TIMEOUT ;
13471348 }
13481349 }
13491350 // If this is a new device or hasn't connected through SDL we want to limit the exposure
@@ -1517,26 +1518,21 @@ private void safeStartForeground(int id, Notification notification){
15171518 private void exitForeground (){
15181519 synchronized (NOTIFICATION_LOCK ) {
15191520 if (isForeground && !isPrimaryTransportConnected ()) { //Ensure that the service is in the foreground and no longer connected to a transport
1520- this .stopForeground (true );
1521+ DebugTool .logInfo ("SdlRouterService to exit foreground" );
1522+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
1523+ this .stopForeground (Service .STOP_FOREGROUND_DETACH );
1524+ }else {
1525+ stopForeground (false );
1526+ }
15211527 NotificationManager notificationManager = (NotificationManager )getSystemService (Context .NOTIFICATION_SERVICE );
1522- if (notificationManager != null && Build . VERSION . SDK_INT >= Build . VERSION_CODES . O ) {
1528+ if (notificationManager != null ) {
15231529 try {
1524- boolean notificationHasDisplayed = false ;
1525- StatusBarNotification [] notifications = notificationManager .getActiveNotifications ();
1526- for (StatusBarNotification notification : notifications ) {
1527- if (notification != null && FOREGROUND_SERVICE_ID == notification .getId ()){
1528- DebugTool .logInfo ("Service notification is being displayed" );
1529- notificationHasDisplayed = true ;
1530- break ;
1531- }
1532- }
1533- if (notificationHasDisplayed ) {
1530+ notificationManager .cancelAll ();
1531+ if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
15341532 notificationManager .deleteNotificationChannel (SDL_NOTIFICATION_CHANNEL_ID );
15351533 }
1536- //else leave the notification channel alone to avoid deleting it before the
1537- //foreground service notification has a chance to be displayed.
1538- } catch (Exception e ){
1539- DebugTool .logError ("Issue when deleting notification channel" , e );
1534+ } catch (Exception e ) {
1535+ DebugTool .logError ("Issue when removing notification and channel" , e );
15401536 }
15411537 }
15421538 isForeground = false ;
0 commit comments