cancel specific notifications instead of using cancelAll

This commit is contained in:
Akinwale Ariwodola 2018-09-02 17:54:37 +01:00
parent 0592c42df0
commit e8185e0f9c
3 changed files with 24 additions and 4 deletions

View file

@ -38,6 +38,8 @@ import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
@ -66,6 +68,8 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
public static final String SETTING_KEEP_DAEMON_RUNNING = "keepDaemonRunning";
public static List<Integer> downloadNotificationIds = new ArrayList<Integer>();
/**
* Flag which indicates whether or not the service is running. Will be updated in the
* onResume method.
@ -294,7 +298,13 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancelAll();
notificationManager.cancel(BackgroundMediaModule.NOTIFICATION_ID);
notificationManager.cancel(DownloadManagerModule.GROUP_ID);
if (downloadNotificationIds != null) {
for (int i = 0; i < downloadNotificationIds.size(); i++) {
notificationManager.cancel(downloadNotificationIds.get(i));
}
}
super.onDestroy();
if (mReactInstanceManager != null) {

View file

@ -24,7 +24,7 @@ import io.lbry.browser.R;
public class BackgroundMediaModule extends ReactContextBaseJavaModule {
private static final int NOTIFICATION_ID = -2;
public static final int NOTIFICATION_ID = 30;
public static final String ACTION_PLAY = "io.lbry.browser.ACTION_MEDIA_PLAY";

View file

@ -46,7 +46,7 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
public static final String NOTIFICATION_ID_KEY = "io.lbry.browser.notificationId";
public static final int GROUP_ID = 0;
public static final int GROUP_ID = 20;
private static NotificationCompat.Builder groupBuilder = null;
@ -58,7 +58,13 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
}
private int generateNotificationId() {
return new Random().nextInt();
int id = 0;
Random random = new Random();
do {
id = random.nextInt();
} while (id < 100);
return id;
}
@Override
@ -225,6 +231,10 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
editor.apply();
}
if (MainActivity.downloadNotificationIds != null &&
!MainActivity.downloadNotificationIds.contains(notificationId)) {
MainActivity.downloadNotificationIds.add(notificationId);
}
downloadIdNotificationIdMap.put(id, notificationId);
return notificationId;
}