How to Create Persistent Notifications in Android 14 for a Location Tracking App?

  Kiến thức lập trình

I’m developing an Android app that relies on persistent notifications to track location. Previously, I used

setOngoing(true)

to make the notification non-dismissible. However, with Android 14, users can now dismiss these notifications even if setOngoing(true) is set.

Here is the relevant documentation about this behavior change: Android 14 Behavior Changes – Non-Dismissable Notifications.

Here’s my current code for the notification:

private Notification getNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Location Service")
            .setContentText("Running")
            .setSmallIcon(R.mipmap.trailmaplogo_foreground)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    builder.setOngoing(true);
    return builder.build();
}

In my app, it’s crucial to have a persistent notification that cannot be dismissed by the user under normal circumstances using swipe control, as it is not a media app but a location tracking app. Given the changes in Android 14, how can I achieve this?

New contributor

Lyrics Zone Unlimited is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT