@@ -1517,26 +1517,17 @@ private void safeStartForeground(int id, Notification notification){
15171517 private void exitForeground (){
15181518 synchronized (NOTIFICATION_LOCK ) {
15191519 if (isForeground && !isPrimaryTransportConnected ()) { //Ensure that the service is in the foreground and no longer connected to a transport
1520- this .stopForeground (true );
1520+ DebugTool .logInfo ("SdlRouterService to exit foreground" );
1521+ this .stopForeground (false ); //false is used because some phones have issues when trying to clear the notification at the same time
15211522 NotificationManager notificationManager = (NotificationManager )getSystemService (Context .NOTIFICATION_SERVICE );
1522- if (notificationManager != null && Build . VERSION . SDK_INT >= Build . VERSION_CODES . O ) {
1523+ if (notificationManager != null ) {
15231524 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 ) {
1525+ notificationManager .cancel (FOREGROUND_SERVICE_ID );
1526+ if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
15341527 notificationManager .deleteNotificationChannel (SDL_NOTIFICATION_CHANNEL_ID );
15351528 }
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 );
1529+ } catch (Exception e ) {
1530+ DebugTool .logError ("Issue when removing notification and channel" , e );
15401531 }
15411532 }
15421533 isForeground = false ;
0 commit comments