Always cancel the persistent notification when the app task is removed

This commit is contained in:
Alexander Bakker 2020-01-02 13:03:45 +01:00
parent 35a14d3198
commit 4cab92d615

View file

@ -17,21 +17,16 @@ public class NotificationService extends Service {
private static final String CODE_LOCK_STATUS_ID = "lock_status_channel"; private static final String CODE_LOCK_STATUS_ID = "lock_status_channel";
private static final String CODE_LOCK_VAULT_ACTION = "lock_vault"; private static final String CODE_LOCK_VAULT_ACTION = "lock_vault";
public NotificationService() {
}
@Override @Override
public int onStartCommand(Intent intent,int flags, int startId){ public int onStartCommand(Intent intent,int flags, int startId){
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
serviceMethod(); serviceMethod();
return Service.START_STICKY; return Service.START_STICKY;
} }
public void serviceMethod() { public void serviceMethod() {
Intent intentAction = new Intent(CODE_LOCK_VAULT_ACTION); Intent intentAction = new Intent(CODE_LOCK_VAULT_ACTION);
PendingIntent lockDatabaseIntent = PendingIntent.getBroadcast(this,1,intentAction, 0); PendingIntent lockDatabaseIntent = PendingIntent.getBroadcast(this, 1, intentAction, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CODE_LOCK_STATUS_ID) NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CODE_LOCK_STATUS_ID)
.setSmallIcon(R.drawable.ic_fingerprint_black_24dp) .setSmallIcon(R.drawable.ic_fingerprint_black_24dp)
.setContentTitle(getString(R.string.app_name_full)) .setContentTitle(getString(R.string.app_name_full))
@ -46,10 +41,15 @@ public class NotificationService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(VAULT_UNLOCKED_ID); notificationManager.cancel(VAULT_UNLOCKED_ID);
super.onDestroy();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
} }
@Nullable @Nullable