


The notification of a foregraound service cannot be cleared. This code works for me: this.stopForeground(false) Īnd you should set your return value of onStartCommand, STRAT_STICKY will restart your service. check that intent != null, intent.getExtras() != null, then extract the value from the extras bundle given someKey, and if it matches someIntValue, then call stopSelf(). Handle the delivered extra within onStartCommand, i.e. When the user swipes it away, the intent with its extra is delivered to the service. PendingIntent deletePendingIntent = PendingIntent.getService(this, Where deletePendingIntent is something like Intent deleteIntent = new Intent(this, YourService.class) ĭeleteIntent.putExtra(someKey, someIntValue) (new NotificationCompat.builder(this).setDeleteIntent(deletePendingIntent)).build() For pre-Lollipop, you may have to stopForeground(true), which stops foreground and removes the notification, then re-issue the notification with notificationManager.notify(yourNotificationID, yourNotificationObject), so that your notification is visible but swipeable.Ĭrucially, set up the notification object with a delete intent that is triggered when the user swipes it away. Hence, stopForeground(false) first, which allows the notification to be swiped away by the user thereafter (at least on Lollipop+). The user is not allowed to swipe away a notification generated by an ongoing foreground service.
