Open In App

How to Dismiss Running App Notifications in Android 13?

Last Updated : 26 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

You may be accustomed to seeing the default notifications which every running app shows you while it is running on your device, like your watch companion app, or any other app, which shows you notifications, but that is history now, as from Android 13, those notifications can be just simply swiped like any other normal notification. This could be a nightmare for a few developers out there, so in this article, we will be looking forward to how to prevent this, or at least adapt our apps to this new behavior.

Users can by default dismiss the notification connected to a foreground service as of Android 13 (API level 33). In order to do this, consumers swipe the information. The foreground service must be disabled or removed from the forefront in order to dismiss the notice on earlier versions of Android. When creating your notice using Notification.Builder, pass ‘true’ into the setOngoing() method if you want the notification to be non-dismissable by the user.

GeekTip #1: If the user disables notifications on Android 13 (API level 33) or higher, they will still be able to see notifications for foreground services in the Foreground Services (FGS) Task Manager but not on the notification drawer.

Services that provide timely notifications

Even on devices running Android 12 or higher, the system displays the related notification as soon as the foreground service launches if it possesses at least one of the following features:

  • Based on the notification’s category attribute, the service offers a use case for media playback, navigation, or phone calls.
  • By supplying FOREGROUND SERVICE IMMEDIATE into setForegroundServiceBehavior() when configuring the notification, the service has chosen not to participate in the behavior change.
  • A notice with action buttons is connected to the service.
  • The foregroundServiceType of the service is either mediaPlayback, mediaProjection, or phoneCall.

How to start a foreground service properly in Android 13

You have the option to ask for your service to operate in the foreground inside the service, typically in onStartCommand(). Call startForeground to do this (). The Notice object itself and a positive integer that uniquely identifies the notification in the status bar are the two inputs for this method, to start your foreground service simply call this method in your Java activity:

Java




Context gfgContext = getApplicationContext();
Intent intent = new Intent (GeeksForGeeks_AcvivityOne.this, GeeksForGeeks_AcvivityOne.class)  
context.startForegroundService(intent);


Draw forward the Android 13 Foreground Service

The following code snippet demonstrates how to seek the FOREGROUND SERVICE permission in an app that targets even lower Android Versions and is emphasized in Android 13 even more, and uses foreground services. Since this is typical permission, the system gives it to the requesting program without prompting. To draw the permission, we will simply update our Manifest file where we keep a track of all the permissions which we request for our app, simply add this permission to your Manifest:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

This is similar to the Foreground Service which we have used even before but works differently when we set the target SDK to Android 13.

GeekTip #2: The system throws a SecurityException if an app targeting API level 28 or higher tries to start a foreground service without obtaining the FOREGROUND SERVICE permission.

This is everything that we require so that your app can get full advantage of the dismissal of the notification, remember to keep the setOngoing to true so that the system knows that it does not need to dismiss that notification.

Conclusion

Except in a few limited circumstances, apps targeting Android 13 (API level 32) or higher cannot initiate foreground services while they are operating in the background. The system throws a ForegroundServiceStartNotAllowedException if an app tries to start a foreground service while the app is operating in the background and the foreground service doesn’t satisfy one of the exceptional conditions.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads