Skip to content

Commit 870a118

Browse files
committed
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.
1 parent 7e6a16c commit 870a118

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import android.os.ParcelFileDescriptor;
7070
import android.os.Parcelable;
7171
import android.os.RemoteException;
72+
import android.service.notification.StatusBarNotification;
7273
import android.support.annotation.NonNull;
7374
import android.support.v4.app.NotificationCompat;
7475
import android.util.Log;
@@ -1516,6 +1517,27 @@ private void exitForeground(){
15161517
synchronized (NOTIFICATION_LOCK) {
15171518
if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport
15181519
this.stopForeground(true);
1520+
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
1521+
if (notificationManager!= null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
1522+
try {
1523+
boolean notificationHasDisplayed = false;
1524+
StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
1525+
for (StatusBarNotification notification : notifications) {
1526+
if(notification != null && FOREGROUND_SERVICE_ID == notification.getId()){
1527+
DebugTool.logInfo("Service notification is being displayed");
1528+
notificationHasDisplayed = true;
1529+
break;
1530+
}
1531+
}
1532+
if (notificationHasDisplayed) {
1533+
notificationManager.deleteNotificationChannel(SDL_NOTIFICATION_CHANNEL_ID);
1534+
}
1535+
//else leave the notification channel alone to avoid deleting it before the
1536+
//foreground service notification has a chance to be displayed.
1537+
} catch (Exception e){
1538+
DebugTool.logError("Issue when deleting notification channel", e);
1539+
}
1540+
}
15191541
isForeground = false;
15201542
}
15211543
}

0 commit comments

Comments
 (0)