Skip to content

Commit 6c24b9e

Browse files
Merge pull request #1271 from smartdevicelink/bugfix/notification_removal_delay
Delete notification channel when possible
2 parents 4ccf293 + 024cebb commit 6c24b9e

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 2 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;
@@ -138,7 +139,7 @@ public class SdlRouterService extends Service{
138139
/**
139140
* <b> NOTE: DO NOT MODIFY THIS UNLESS YOU KNOW WHAT YOU'RE DOING.</b>
140141
*/
141-
protected static final int ROUTER_SERVICE_VERSION_NUMBER = 10;
142+
protected static final int ROUTER_SERVICE_VERSION_NUMBER = 11;
142143

143144
private static final String ROUTER_SERVICE_PROCESS = "com.smartdevicelink.router";
144145

@@ -1443,8 +1444,9 @@ private void enterForeground(String content, long chronometerLength, boolean ong
14431444
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
14441445
builder.setContentIntent(pendingIntent);
14451446

1446-
if(chronometerLength > 0 && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
1447+
if(chronometerLength > (FOREGROUND_TIMEOUT/1000) && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
14471448
//The countdown method is only available in SDKs >= 24
1449+
// Only add countdown if it is over the min timeout
14481450
builder.setWhen(chronometerLength + System.currentTimeMillis());
14491451
builder.setUsesChronometer(true);
14501452
builder.setChronometerCountDown(true);
@@ -1516,6 +1518,27 @@ private void exitForeground(){
15161518
synchronized (NOTIFICATION_LOCK) {
15171519
if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport
15181520
this.stopForeground(true);
1521+
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
1522+
if (notificationManager!= null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
1523+
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) {
1534+
notificationManager.deleteNotificationChannel(SDL_NOTIFICATION_CHANNEL_ID);
1535+
}
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);
1540+
}
1541+
}
15191542
isForeground = false;
15201543
}
15211544
}

android/sdl_android/src/main/res/values/sdl.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<resources>
33
<string name="sdl_router_service_version_name" translatable="false">sdl_router_version</string>
44

5-
<integer name="sdl_router_service_version_value">10</integer>
5+
<integer name="sdl_router_service_version_value">11</integer>
66

77
<string name="sdl_router_service_is_custom_name" translatable="false">sdl_custom_router</string>
88
</resources>

0 commit comments

Comments
 (0)